Returning Multiples of SUM when joining two tables - sql-server-2005

When I join these two tables the sum results are multiples of the number of rows in the tables being queried. For instance, sum(jle.Unit Price) should be $834,485.00. However, it returns a value of $7760710.50. It is summing the lines from that table 31 times, the same number, minus one, of rows in the jpl table. The same for the sum on jpl.[Total Price]. Returning 3 times (for the three rows in jle table) than the result should be. What am I doing wrong. I want to add this to a query that is already working based on the Job No_ field.
select jle.[Job No_], sum(jle.[Unit Price]) as 'Invoiced Amount', sum(jpl.[Total Price])as 'Sale $'
from [Job Ledger Entry] jle join [Job Planning Line] jpl on jle.[Job No_] = jpl.[Job No_]
where jle.[Job No_] = 'j-2397'
group by jle. [Job No_]

Try this.
select jpl.[Job No_], SUM(jle.[Unit Price]) as 'Invoiced Amount', MIN(jpl.[Total Price]) AS 'Sale $'
from [Job Planning Line] jpl INNER JOIN (SELECT DISTINCT [Job No_], [Unit Price] FROM [Job LEdger Entry]) AS jle on jle.[Job No_] = jpl.[Job No_]
where jpl.[Job No_] = 'j-2397'
group by jpl. [Job No_]

Related

Get latest Vendor No. in sql query from Item Ledger Entry table

I'm trying to get last Vendor No. from Item Ledger Entry table with no result.
What should I put to my query to get this latest Vendor No.?
SQL query looks as follow:
SELECT
ile.[Posting Date],
CASE
WHEN ile.[Entry Type]=1 THEN 'Sale'
WHEN ile.[Entry Type]=5 THEN 'Consumption'
END AS Entry_Type,
,ile.[Item No_]
,ile.[Description]
,ile.[Quantity]
,ile.[Unit of Measure Code]
,item.[Description]
--,Last Vendor No.
FROM [MyServer]..[MyDatabase$Item Ledger Entry] as ile with (nolock)
LEFT JOIN [MyServer]..[MyDatabase$Item] as item ON item.[No_]=ile.[Item No_]
--LEFT JOIN
-- place for query to find last Vendor No. for particular Item No.
-- ... FROM [MyServer]..[MyDatabase$Item Ledger Entry] WHERE [Entry Type]=0 and [Source Type]=2
--
WHERE ile.[Entry Type] In (1,5) AND ile.[Item No_] ='123456789';
select top 1 [Source No_]
from [DatabaseName].dbo.[CompanyName$Item Ledger Entry]
where [Source Type] = 2 /* Vendor */
and [Entry Type] = 0 /* Purchase */
and [Item No_] = '<Some Item No.>'
order by [Entry No_] desc

How to Filter Data in SQL Without Using Group By / Having Functions

Have a situation where I'm trying to produce a query that shows the item # on a sales order and the total outstanding quantity across all orders. I can do this as follows:
SELECT
SL.[Item No_],
SUM(SL.[Outstanding Quantity])
FROM [Database$Sales Header] SH
LEFT JOIN [Database$Sales Line] SL ON SL.[Document No_] = SH.[No_]
LEFT JOIN [Database$Items] I ON I.[No_] = SL.[Item No_]
GROUP BY
SL.[Item No_],
SH.[Document Type],
I.[Product Code]
HAVING
SH.[Document Type] = '1'
AND I.[Product Code] = 'SHIRT'
ORDER BY
SL.[Item No_]
Above code gives me a simple summary of item # and qty. on all sales orders.
I'm using the HAVING clause to include only sales orders (Document Type)
and only items that are shirts (Product Code).
The issue I'm having is when I want to exclude a particular customer.
I tried adding: AND SH.[Customer No_] <> 'CUST1' to the HAVING clause but if I do that, then SQL will require me to add it in the GROUP BY clause. The result is I get duplicate rows where it was summarized before because now SQL is reporting on outstanding quantity by item # and customer # which is not what I want.
So I am not sure how to exclude that customer without putting it into GROUP BY.
Please use where clause to filter the data.
SELECT
SL.[Item No_],
SUM(SL.[Outstanding Quantity])
FROM [Database$Sales Header] SH
LEFT JOIN [Database$Sales Line] SL ON SL.[Document No_] = SH.[No_]
LEFT JOIN [Database$Items] I ON I.[No_] = SL.[Item No_]
where SH.[Customer No_] <> 'CUST1'
GROUP BY
SL.[Item No_],
SH.[Document Type],
I.[Product Code]
HAVING
SH.[Document Type] = '1'
AND I.[Product Code] = 'SHIRT'
ORDER BY
SL.[Item No_]

SQL query question from someone with no SQL training

I'm very new to SQL and have what I hope is a simple question. At the moment this query returns the same value for [quantity] if a [Lot No_] is in multiple [Bin codes]. I would like this query to sum the quantity per [bin code],
EDIT
It seems like the query sums up all entries in the warehouse table and returns that same quantity value for all the entries in the new table.
i.e currently if warehouse table has;
[Quantity] [Bin code] [Lot No_]
40 A x
-35 A x
15 B x
-15 B x
5 C x
5 C x
It gives the following table (when added with master$bin table)
[Quantity] [Bincode] [Lot No_] [Item No]
-------------------------------------------
30 A X f1
30 B X f2
30 C X f3
Would like it to give the following;
[Quantity] [Bincode] [Lot No_] [Item No]
-------------------------------------------
05 A X f1
15 B X f2
10 C X f3
Thanks in advance, this is the query;
ALTER PROCEDURE [dbo].[sp_QV_Lots]
AS
TRUNCATE TABLE [QV_Lots];
INSERT INTO [QV lots]
--select quantity, [Lot No_], [Bin Code], [Item No] from [QV Lots]
SELECT
SUM(Warehouse.Quantity) AS Quantity,
BinContent.[Lot No_],
BinContent.[Bin Code],
Bincontent.[Item No_]
FROM
[DB01].[LiveNLic].[dbo].[Master$Bin Content] AS BinContent
INNER JOIN
[DB01].[LiveNLic].dbo.[Warehouse Entry] AS Warehouse ON Warehouse.[Lot No_] = BinContent.[Lot No_]
WHERE
(BinContent.[Location Code] = 'A' OR BinContent.[Location Code] = 'B')
GROUP BY
BinContent.[Lot No_], BinContent.[Bin Code], Bincontent.[Item No_]
Your query and your sample results are not compatible (one has three columns, one has four). Your query is not what produces those results.
So, I suspect that you also have quantity or something else in the group by.
The following query should be calculating only the quantity associated with each row:
SELECT SUM(w.Quantity) AS Quantity,
bc.[Lot No_], bc.[Bin Code]
FROM [DB01].[LiveNLic].[dbo].[Master$Bin Content] bc JOIN
[DB01].[LiveNLic].dbo.[Warehouse Entry] w
ON w.[Lot No_] = bc.[Lot No_]
WHERE bc.[Location Code] IN ('A', 'B')
GROUP BY bc.[Lot No_], bc.[Bin Code]

Unexpected NULLs in JOIN query

I am using 3 tables in the query: 2 with INNER JOIN and the 3rd with LEFT JOIN.
However, I am getting some NULLrecords while the LEFT JOIN condition is executing. There are coming from the Purchase line(PL_ TABLE)`which should not be the case.
Can any one recommend the change in the query to replace NULL values in the record with actual values?
Sorry, I could not find any option to attach the sample table data.
The Query
SELECT
pih.[PO Number],
pih.[Pre-Assigned No_] as [Invoice No],
pil.[Document No_],
pil.[Description] as [Reason For Discrepency],
pil.[Line No_],
pl.[No_] as [Item No],
pl.[Vendor Item No_],
pl.[Order Date],
pil.[Posting Date],
pil.[Expected Receipt Date],
pih.[Notes] as [Header Notes],
pil.[No_] as [G/L Account No],
pih.[Buy-from Vendor No_],
pih.[Buy-from Vendor Name],
Pil.Quantity as [Inv Qty From InvoiceLine],
pil.[Amount Including VAT] as [Inv Value From InvoiceLine],
pl.Quantity as [PO Quantity From Purchaseline],
pl.[Quantity Received] as [Received Qty From PurchaseLine],
pl.[Quantity Invoiced] as [Invoiced Qty From PurchaseLine] ,
pl.[Amount Including VAT] as [PO Value From PurchaseLine]
FROM
[Purch_ Inv_ Line] pil
INNER JOIN Purch_ Inv_ Header] pih
ON pil.[Document No_] = pih.[No_]
LEFT JOIN [Purchase Line] pl
ON pih.[PO Number]=pl.[Document No_] and pl.[Line No_]=pil.[Line No_]
WHERE
PIL.[Document No_] IN
(
SELECT distinct pil.[Document No_] FROM
Purch_ Inv_ Line] pil
WHERE piL.[No_] in ('700xxx','700xxx','17xxxxx') and pil.[Posting Date] >=getdate()-7
)
AND piL.[Type]='1'
There might be 2 reasons why you are getting NULL values:
Values of columns in [Purchase line] table are actually NULL
There are no matching rows in [Purchase line] table for rows that are NULL. This is how LEFT JOIN works
If you want to get only matching rows, then change LEFT JOIN to INNER JOIN - however then your result set will not contain rows from [Purch_ Inv_ Line] and [Purch_ Inv_ Header] where there are not matching [Purchase line] rows.
If you want to give default values to NULL values you can do it using ISNULL:
ISNULL(pl.[Amount Including VAT], 0) as [PO Value From PurchaseLine]

How to SQL join payments with invoices in NAV Dynamics (former Navision)

The question is about SQL fiddling in Dynamics Nav tables, especially the table [$Vendor Ledger Entry]. How to link each payment to appropriate invoices.
The aim is to get [External Document No_] and [Amount] of the invoice. One payment can be matched with many invoices (happens) or one invoice with many payments (rare case).
My desperate approach is:
;with cte as
(
select
*
,[Row_max]=max([Row]) OVER(PARTITION BY CBEN,[Document Type])
from (
select
*
,[Row]=ROW_NUMBER() OVER(PARTITION BY CBEN,[Document Type] ORDER BY [Entry No_] asc)
from (
select
*
,CBEN=case when [Closed by Entry No_]=0 then [Entry No_] else [Closed by Entry No_] end
from [CompanyName$Vendor Ledger Entry] --here put correct table name
) tb1
) tb2
)
select
a.[Entry No_] [Entry No_payment] -- letter t means transfers
,b.[Entry No_] [Entry No_invoice] -- letter p means payables
--,a.CBEN as CBEN_t
--,b.CBEN as CBEN_p
--,a.[Row_max] as Row_max_t
--,b.[Row_max] as Row_max_p
--,a.[Row] as Row_t
--,b.[Row] as Row_p
,a.[Open]
,b.[External Document No_] [External Document No_invoice]
,a.[Document No_] [Document No_payment]
,b.[Document No_] [Document No_invoice]
,b.[Document Date]
,b.[Posting Date]
,b.[Due Date]
,Amount_p=b.[Closed by Amount]
--,a.[Company]
from cte a
left join cte b
on
(
a.CBEN=b.CBEN and -- join using Closed By Entry No
(a.[Row]=b.[Row] and a.[Row_max]=b.[Row_max] or
a.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1) or
b.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1)
) and
a.[Document Type] in (0,1) and
b.[Document Type]=2
and a.[Open]=0
)
or
(
a.CBEN = b.[Entry No_] and -- or join by Closed By Entry No to EntryNo
(a.[Row]=b.[Row] and a.[Row_max]=b.[Row_max] or
a.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1) or
b.Row_max=1 and not (a.[Row_max]>1 and b.[Row_max]>1)
) and
a.[Document Type] in (0,1) and
b.[Document Type]=2
and a.[Open]=0
)
where
a.[Open]=0 and a.[Document Type] in (0,1) and b.[Entry No_] is not null -- show payments with matched invoices
or
a.[Open]=1 and a.[Document Type] in (0,1) and b.[Entry No_] is null -- or payments with open status
order by a.CBEN,1,2,a.[Document No_]
I am at a loss to join Invoice [Amount] and I do not know in which Dynamics NAV table it might be.
the table is
[Detailed Vendor Ledg. Entry]
with [Entry Type] = Application