This script is part of the SQL Scripts for Microsoft Dynamics GP where I will be posting the scripts I wrote against Microsoft Dynamics GP over the 19 years before I stopped working with Dynamics GP.
This script returns a list of sales in the last five years summarised by salesperson:
/*
Created by Ian Grieve of azurecurve | Ramblings of an IT Professional (http://www.azurecurve.co.uk)
This code is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0 Int).
*/
SELECT
ISNULL(['RM Salesperson Master'].SLPRSNID, '* No Salesperson *') AS CustomerSalesperson
,RTRIM(LTRIM(RTRIM(ISNULL(['RM Salesperson Master'].SLPRSNFN, '')) + ' ' + ISNULL(['RM Salesperson Master'].SPRSNSLN, ''))) AS CustomerSalesPersonName
,SUM(CASE WHEN YEAR(['Sales Data'].DOCDATE) = YEAR(GETDATE()) THEN SLSAMNT ELSE 0 END) AS SalesYear0
,SUM(CASE WHEN YEAR(['Sales Data'].DOCDATE) = YEAR(GETDATE()) -1 THEN SLSAMNT ELSE 0 END) AS [SalesYear-1]
,SUM(CASE WHEN YEAR(['Sales Data'].DOCDATE) = YEAR(GETDATE()) -2 THEN SLSAMNT ELSE 0 END) AS [SalesYear-2]
,SUM(CASE WHEN YEAR(['Sales Data'].DOCDATE) = YEAR(GETDATE()) -3 THEN SLSAMNT ELSE 0 END) AS [SalesYear-3]
,SUM(CASE WHEN YEAR(['Sales Data'].DOCDATE) = YEAR(GETDATE()) -4 THEN SLSAMNT ELSE 0 END) AS [SalesYear-4]
FROM
(
SELECT
CUSTNMBR
,DOCNUMBR
,RMDTYPAL
,DOCDATE
,GLPOSTDT
,CASE WHEN RMDTYPAL < 7 THEN
SLSAMNT
ELSE
SLSAMNT * -1
END AS SLSAMNT
,SLPRSNID
FROM
RM20101 [sqlgreen]--[gptl=RM20101][/sqlgreen]
WHERE
RMDTYPAL <> 9
AND
VOIDSTTS = 0
UNION ALL
SELECT
CUSTNMBR
, DOCNUMBR
, RMDTYPAL
, DOCDATE
, GLPOSTDT
,CASE WHEN RMDTYPAL < 7 THEN
SLSAMNT
ELSE
SLSAMNT * -1
END AS SLSAMNT
,SLPRSNID
FROM
RM30101 [sqlgreen]--[gptl=RM30101][/sqlgreen]
WHERE
RMDTYPAL <> 9
AND
VOIDSTTS = 0
) AS ['Sales Data']
LEFT OUTER JOIN
RM00301 AS ['RM Salesperson Master'] --RM Salesperson Master (RM00301)
ON
['Sales Data'].SLPRSNID = ['RM Salesperson Master'].SLPRSNID
GROUP BY
['RM Salesperson Master'].SLPRSNID
,['RM Salesperson Master'].SLPRSNFN
,['RM Salesperson Master'].SPRSNSLN
GO