Joins with aggregates doubling, sometimes tripling quantity amounts - sql-server-2005

I'm trying to join 4 tables to get several columns of results, two of which are sums/aggregates of their respective columns. My query is returning multiples of what the true sums should be. Here is what I have:
select pl.[Vendor Item No_], bc.[Item No_], min(ile.[Description]) as 'Item Description',
sum(ile.[Quantity]) as 'Quantity On Hand',
bc.[Bin Code] as 'Item Location'
from [live$Bin Content]bc left outer join [live$purchase line]pl
on bc.[Item No_] = pl.[No_]left outer join [live$item ledger entry] ile
on bc.[Item No_] = ile.[Item No_]
where bc.[Bin Code] like 'ANNEX BACK'
and bc.[Item No_] like 'sk%'
group by pl.[Vendor Item No_], bc.[Item No_], pl.[Description], bc.[Bin Code]

using a subquery/inline view may solve your problem. assuming everything else is working. To know for certain we would need to know the PK/FK relationship between all 3 tables.
SELECT pl.[Vendor Item No_],
bc.[Item No_],
min(ile.[Description]) as 'Item Description',
ile.[Quantity] as 'Quantity On Hand',
bc.[Bin Code] as 'Item Location'
FROM [live$Bin Content] bc
LEFT JOIN [live$purchase line] pl
on bc.[Item No_] = pl.[No_]
LEFT JOIN (SELECT sum(quantity) as Quantity, [Item no_]
FROM [live$item ledger entry]
GROUP BY [Item no_]) ile
on bc.[Item No_] = ile.[Item No_]
where bc.[Bin Code] like 'ANNEX BACK'
and bc.[Item No_] like 'sk%'
group by pl.[Vendor Item No_], bc.[Item No_], pl.[Description], bc.[Bin Code]
Per comment... if you want to add another table and aggregrate quantity...
SELECT pl.[Vendor Item No_],
bc.[Item No_],
min(ile.[Description]) as 'Item Description',
ile.[Quantity] as 'Quantity On Hand',
bc.[Bin Code] as 'Item Location'
FROM [live$Bin Content] bc
LEFT JOIN [live$purchase line] pl
on bc.[Item No_] = pl.[No_]
LEFT JOIN (SELECT sum(quantity) as Quantity, [Item no_]
FROM [live$item ledger entry]
GROUP BY [Item no_]) ile
on bc.[Item No_] = ile.[Item No_]
LEFT JOIN (SELECT sum(NEWFIELD) as Quantity, [Item no_]
FROM [newTable]
GROUP BY [Item no_]) newAlias
on bc.[Item No_] = newAlias.[Item No_]
where bc.[Bin Code] like 'ANNEX BACK'
and bc.[Item No_] like 'sk%'
group by pl.[Vendor Item No_], bc.[Item No_], pl.[Description], bc.[Bin Code]
However if the field is in one of the existing tables, you just need to add it as a new field on the subquery...
SELECT pl.[Vendor Item No_],
bc.[Item No_],
min(ile.[Description]) as 'Item Description',
ile.[Quantity] as 'Quantity On Hand',
bc.[Bin Code] as 'Item Location',
ile.count as count of items with inventory in ILE.
FROM [live$Bin Content] bc
LEFT JOIN [live$purchase line] pl
on bc.[Item No_] = pl.[No_]
LEFT JOIN (SELECT sum(quantity) as Quantity, count(Quantity) as count, [Item no_]
FROM [live$item ledger entry]
GROUP BY [Item no_]) ile
on bc.[Item No_] = ile.[Item No_]
where bc.[Bin Code] like 'ANNEX BACK'
and bc.[Item No_] like 'sk%'
group by pl.[Vendor Item No_], bc.[Item No_], pl.[Description], bc.[Bin Code]

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_]

Inner join query don't retrieve the most recent result

I need to update my Item field "Vendor No." according to the most recent result of the field "Source No." of table "Item ledger Entry"
I tried to do a inner join between both tables (item and item ledger entry) but some of the results of this query was not the most recent.
So If i do this query with a specific case,
select top(1)
[MR$Item].[No_],
[MR$Item Ledger Entry].[Item No_],
[MR$Item].[Vendor No_],
[MR$Item Ledger Entry].[Source No_],
[MR$Item Ledger Entry].[Entry Type],
[MR$Item Ledger Entry].[Posting Date]
from [MR$Item]
left outer join [MR$Item Ledger Entry] on [MR$Item].[No_] = [MR$Item Ledger Entry].[Item No_]
where
[MR$Item Ledger Entry].[Entry Type] = 0
and [MR$Item].[Vendor No_] <> [MR$Item Ledger Entry].[Source No_]
AND year([MR$Item Ledger Entry].[Posting Date])>=2018
AND [MR$Item].[No_] = '3510100011'
order by [MR$Item Ledger Entry].[Posting Date] DESC
I get this results
No_ Item No_ Vendor No_ Source No_ Entry Type Posting Date
3510100011 3510100011 505881497 172140064 0 2018-09-27 00:00:00.000
But doing this simple select, I can see that the last result is not right
select top(1)
[MR$Item Ledger Entry].[Item No_],
[MR$Item Ledger Entry].[Source No_],
[MR$Item Ledger Entry].[Entry Type],
[MR$Item Ledger Entry].[Posting Date]
from [MR$Item Ledger Entry]
where
[MR$Item Ledger Entry].[Entry Type] = 0 AND
year([MR$Item Ledger Entry].[Posting Date]) >= 2018 AND
[MR$Item Ledger Entry].[Item No_] = '3510100011'
order by [MR$Item Ledger Entry].[Posting Date] DESC
The results are
Item No_ Source No_ Entry Type Posting Date
3510100011 508606977 0 2018-01-09 00:00:00.000
And the correct result is for the item "3510100011" I should have the number "508606977".
What am I doing wrong?
Because in your query, you put :
AND [MR$Item Ledger Entry].[Item No_]='3510100011'
And, you can change your left join to inner join, because you are doing where on the table [MR$Item Ledger Entry]

SQL IsNull with SELECT subquery in a query

I have this:
ISNULL(FPONO.[Replan Ref_ No_],(SELECT DISTINCT PO.[Replan Ref_ No_]
FROM NAV_Vermorel_Live.dbo.[SC Vermorel SRL$Production Order] AS PO
WHERE SH.[External Document No_] = PO.[Old Prod_ Order No_] AND PO.[Source No_] = SL.No_))
This piece of SQL is part of big wall of text, which works without the IsNULL check. With IsNUll it outputs the error bellow. Could anyone point me in a right direction? I have null on that specific column, and I can get the right results from another table. I don't know how to do it.
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
SELECT SL.[Document No_],
SL.[Sell-to Customer No_],
SL.Type,
SL.[Line No_],
ISNULL(FPONO.[Replan Ref_ No_],(SELECT DISTINCT PO.[Replan Ref_ No_]
FROM NAV_Vermorel_Live.dbo.[SC Vermorel SRL$Production Order] AS PO
WHERE SH.[External Document No_] = PO.[Old Prod_ Order No_] AND PO.[Source No_] = SL.No_)),
SL.No_,
SL.[Location Code],
SL.[Posting Group],
SL.[Shipment Date],
SL.Description,
SL.[Unit of Measure],
SL.Quantity,
SL.[Outstanding Quantity],
SL.[Qty_ to Invoice],
SL.[Qty_ to Ship],
SL.[Unit Price],
SL.Amount,
SL.[Net Weight],
SL.[Outstanding Amount],
SL.[Qty_ Shipped Not Invoiced],
SL.[Quantity Shipped],
SL.[Quantity Invoiced],
SL.[Gen_ Prod_ Posting Group],
SL.[Line Amount],
SL.[Item Category Code],
SL.[Requested Delivery Date],
SL.[Shipping Time],
SL.[Piece Index],
SL.Urgenta,
SL.[Document Type],
SH.[External Document No_],
Cust.Name
FROM NAV_Vermorel_Live.dbo.[SC Vermorel SRL$Sales Line] AS SL
INNER JOIN NAV_Vermorel_Live.dbo.[SC Vermorel SRL$Sales Header] AS SH ON SL.[Document No_] = SH.No_
INNER JOIN NAV_Vermorel_Live.dbo.[SC Vermorel SRL$Customer] AS Cust ON SL.[Sell-to Customer No_] = Cust.No_
left JOIN (SELECT
RE1."Entry No_",
RE1."Item No_",
RE1."Quantity (Base)",
RE1."Source Subtype",
RE1."Source ID",
RE1."Source Type",
RE1."Source Ref_ No_",
RE2."Source Ref_ No_" AS SRN,
RE2."Source ID" As SalesOrder,
PO.[Replan Ref_ No_]
FROM NAV_Vermorel_Live.dbo."SC Vermorel SRL$Reservation Entry" AS RE1 JOIN NAV_Vermorel_Live.dbo."SC Vermorel SRL$Production Order" AS PO
ON RE1."Source ID" = PO.No_
right JOIN (SELECT
RE."Entry No_",
RE."Item No_",
RE."Quantity (Base)",
RE."Source Subtype",
RE."Source ID",
RE."Source Ref_ No_"
FROM NAV_Vermorel_Live.dbo."SC Vermorel SRL$Reservation Entry" AS RE
WHERE RE."Source Subtype"=1 AND RE.[Source Type] = 37) AS RE2 ON RE1.[Entry No_] = RE2.[Entry No_]
WHERE (RE1."Source Type" = 5406 OR RE1."Source Type" = 5407) AND (RE1."Source Subtype" = 2 OR RE1."Source Subtype" = 3)) AS FPONO
ON (SL.[Document No_] = FPONO.[SalesOrder])
AND (FPONO."SRN" = SL.[Line No_])
AND (FPONO.[Item No_] = SL.[No_])
WHERE (SL.[Outstanding Quantity] > 0)
AND (SL.[Location Code] = 'MACH FIN'
OR SL.[Location Code] = 'MAGAZIN NC'
OR SL.[Location Code] = 'MARFURI')
The message is simple: it is possible to find more than one [Replan Ref_ No_] per [Old Prod_ Order No_] and [Source No_] in your table [SC Vermorel SRL$Production Order].
Either make sure your table doesn't contain duplicates (with a unique constraint) or change the subquery to return one value only, e.g. replace
SELECT DISTINCT PO.[Replan Ref_ No_]
with
SELECT MIN(PO.[Replan Ref_ No_])
You should also be able to use top 1 in your sub query.

Returning Multiples of SUM when joining two tables

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_]