I'm not very familiar with SQL reporting. I know the basics but now I've been given the task of finding out why this query is not returning some proper data.This query creates a list of items from our inventory system and adds up the number of units that appear on an invoice. It's basically a "how many of this item we sold in the first, second, third, or fourth quarter". It's filtered by the Vendor we buy the item from. So the list of items would be filtered by vendor -- Show only the items we buy from vendor XXXX.The problem is that any item which hasn't appeared on an invoice, the price and cost are not being displayed in the report.http://img35.imageshack.us/img35/95/purchrep1.gifYou can see in that screenshot that the items which don't have any data in the "units prior" columns, the price and cost do not show.My task is to get the price and cost to appear.Here is my query which is being done in SAP Business One:SELECT T0.ItemCode, T0.ItemName, T0.U_SES_PageNo, T0.OnHand, T0.IsCommited, T0.OnOrder, (SELECT Price FROM ITM1 WHERE ItemCode = T1.ItemCode AND PriceList = 1) [UnitPrice], (SELECT Price FROM ITM1 WHERE ItemCode = T1.ItemCode AND PriceList = 2) [UnitCost], UTQ = case when datepart(qq,OINV.DocDate) = 04 and datepart(yyyy,OINV.DocDate)=datepart(yyyy,GetDate()) then INV1.Quantity else 0 end, UPQ = case when datepart(qq,OINV.DocDate) = 03 and datepart(yyyy,OINV.DocDate)=datepart(yyyy,GetDate()) then INV1.Quantity else 0 end, UTY = case when datepart(yyyy,OINV.DocDate) = datepart(yyyy,GetDate()) then INV1.Quantity else 0 end, UPY = case when datepart(yyyy,OINV.DocDate) = datepart(yyyy,GetDate())-1 then INV1.Quantity else 0 end INTO ##TEMP FROM OINV INNER JOIN INV1 ON OINV.DocEntry = INV1.DocEntry FULL JOIN OITM T0 ON INV1.ItemCode = T0.ItemCode FULL JOIN ITM1 T1 ON INV1.ItemCode = T1.ItemCode WHERE T0.CardCode = '[%0]' ORDER BY T0.ItemCode SELECT ItemCode AS 'Item Code', ItemName AS 'Description', U_SES_PageNo AS 'Page Number', CAST(OnHand AS INT) AS 'On Hand', CAST(IsCommited AS INT) AS 'Committed', CAST(OnOrder AS INT) AS 'On Order', CAST(SUM(UTQ)/10 AS INT) AS 'Units 4th Qtr', CAST(SUM(UPQ)/10 AS INT) AS 'Units 3rd Qtr', CAST(SUM(UTY)/10 AS INT) AS 'Units This Year', CAST(SUM(UPY)/10 AS INT) AS 'Units Prior Year', UnitCost AS 'Cost', UnitPrice AS 'Price' FROM ##TEMP GROUP BY ItemCode, ItemName, U_SES_PageNo, OnHand, IsCommited, OnOrder, UnitCost, UnitPrice ORDER BY ItemCode DROP TABLE ##TEMP
Item data is stored in the table OITMCost and price are stored in ITM1How many of each item has been sold is stored in OINV and INV1Vendor (CardCode) is stored in OITMI'm pretty sure the tables are not joined properly, but I am unclear on what the proper way would be to join them while still being able to select the numbers sold data, as well as being able to filter it by vendor.Any help is much appreciated!