Hi Experts,
I am looking for some help with a query I use at year end to check turnover for Sales Employees based on the Sales Employee on the BP rather than the Sales Employee in the sales order document. It helps me set targets for the following year. I need to see the amounts for each month of the year. At the moment I have to create a seperate query for each month of the year - I would like to know if it would be possible to add columns to the output for every month of the year rather than have to run a seperate query for each month.
SELECT T0.CardCode, T0.CardName, T4.GroupName, T3.SlpName, Sum(T2.LineTotal) 'YTD Total'
FROM OCRD T0
Left JOIN OINV T1 ON T0.CardCode = T1.CardCode Left JOIN INV1 T2 ON T1.DocEntry = T2.DocEntry Left JOIN OSLP T3 ON T0.SlpCode = T3.SlpCode Left JOIN OCRG T4 ON T0.GroupCode =T4.GroupCode WHERE T0.CardType ='C' and T2.TargetType != 14 And Year(T1.[DocDate]) = 2012 and Month(t1.[docdate])=9 GROUP BY T0.CardCode, T0.CardName, T3.SlpName, T4.GroupName
UNION ALL
SELECT T0.CardCode, T0.CardName, T4.GroupName, T3.SlpName, 0 'YTD Total'
FROM OCRD T0
Left JOIN OSLP T3 ON T0.SlpCode = T3.SlpCode Left JOIN OCRG T4 ON T0.GroupCode =T4.GroupCode WHERE T0.CardType ='C' And T0.CardCode NOT IN(
SELECT T0.CardCode
FROM OCRD T0
Left JOIN OINV T1 ON T0.CardCode = T1.CardCode
Left JOIN INV1 T2 ON T1.DocEntry = T2.DocEntry
Left JOIN OSLP T3 ON T0.SlpCode = T3.SlpCode
Left JOIN OCRG T4 ON T0.GroupCode =T4.GroupCode
WHERE T0.CardType ='C' and T2.TargetType != 14
And Year(T1.[DocDate]) = 2012 and Month(t1.[docdate])=9
)
order by T0.CardCode
Regards
Geoff