I am a novice at writing SQL queries in SAP Business One and generally the basic knowledge I have is sufficient.
In this case I am looking to write a query which will show the current stock position for an Stock item along with the Monthly sales (quantity sales) for the preceding months.
After looking for some time I came across this query on this site;
http://wiki.scn.sap.com/wiki/display/B1/SQL+Queries
I have used the query structure, which works however the results display across a number of lines, with sales quantities appearing on different rows apparently at random. See screen shot below and attached;
![SalesAnalysisQuery20.05.15.jpg]()
I have modified the original query slightly for my purposes - see below;
Declare @Year Numeric
Set @Year='2015'
SELECT T0.ItemCode, T0.ItemName, (T0.[OnHand] - T0.[IsCommited]) + T0.OnOrder AS 'Available',
sum(Case DATENAME(month,T2.DocDate) when 'January' then T1.Quantity else 0 end) as 'January Qty',
sum(Case DATENAME(month,T2.DocDate) when 'February' then T1.Quantity else 0 end) as 'February Qty',
sum(Case DATENAME(month,T2.DocDate) when 'March' then T1.Quantity else 0 end) as 'March Qty',
sum(Case DATENAME(month,T2.DocDate) when 'April' then T1.Quantity else 0 end) as 'April Qty',
sum(Case DATENAME(month,T2.DocDate) when 'May' then T1.Quantity else 0 end) as 'May Qty',
sum(Case DATENAME(month,T2.DocDate) when 'June' then T1.Quantity else 0 end) as 'June Qty',
sum(Case DATENAME(month,T2.DocDate) when 'July' then T1.Quantity else 0 end) as 'July Qty',
sum(Case DATENAME(month,T2.DocDate) when 'September' then T1.Quantity else 0 end) as 'September Qty',
sum(Case DATENAME(month,T2.DocDate) when 'October' then T1.Quantity else 0 end) as 'October Qty',
sum(Case DATENAME(month,T2.DocDate) when 'November' then T1.Quantity else 0 end) as 'November Qty',
sum(Case DATENAME(month,T2.DocDate) when 'December' then T1.Quantity else 0 end) as 'December Qty'
FROM dbo.OITM T0
INNER JOIN INV1 T1 ON T0.ItemCode = T1.ItemCode
INNER JOIN OINV T2 ON T1.DocEntry = T2.DocEntry
WHERE DATENAME(YEAR ,T1.DocDate )=@Year AND T0.ItemCode = '[%0]'
GROUP BY DATENAME(month,T1.DocDate ),T0.ItemCode, T0.ItemName,T0.OnHand,T0.[IsCommited], T0.OnOrder
My question is whether there is any way to amend the query so that the results for an item code appear on a single line. My intention is to run the query on groups of item codes - top sellers for example, so that I can work out whether additional stocks need to be ordered based on previous sales patterns and the current available quantity.
Any help would be greatly appreciated.
Tadhg