Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

PIVOT Rows to Columns based on dates with uneven amounts of rows and columns

$
0
0

I have looked through TechNet and SQL Server Central for a good way to pivot a difficult table. So far, the many different approaches have failed and I need a fresh perspective and help with the code.

I will provide the table language below.

The need is to output a table that will show a salespersons name with the Gross Profit each made in a month for the last 12 months (including the current) and then to take the GP made in the month from the current year and subtract from the GP made in the same month of the prior year.

One of the problems is that the salespersons do not all have the same data available for the same range; but the months must line up in order.  So if a salesperson has no data for a month it should be NULL.  However, every Month1,Month2,Month3, etc needs to be the same actual month year.  So, just iterating through them does not work as the "month1" for someone with data in November as the earliest is not the same as the "month1" for someone with October as the earliest available.

The environment is SQL Server 2008 R2.

The table created will be used as a data source in SSRS 2008 R2.

Sample results:

StartDate: 11/01/2013
EndDate: 11/30/2013

M1 = 11/2013
M2 = 10/2013
M3 = 9/2013
...
M12 = 11/2012

CY = current year
PY = prior year

YOYGPDiff = Year Over Year Gross Profit Diff (CY - PY)

[names] [date per month CY] [Monthly GrossProfit CY] [date per month PY] [Monthly GrossProfit PY] [Monthly YOYGPDiff]

Name            DATE_CY1 MonthlyGP_CY1 DATE_PY1  MonthlyGP_PY1 YOYGPDiff_M1  ...  
BOB BELL       11-01-2013 1333.650000  11-01-2012  10969.280000 -9635.630000  ...  

   
                     DATE_CY12 MonthlyGP_CY12 MonthlyGP_PY12 YOYGPDiff_M12
                     11-01-2012 10969.280000 11254.240000 -284.960000
   
   
   
                     DATE_CY1 MonthlyGP_CY1 DATE_PY1  MonthlyGP_PY1 YOYGPDiff_M1 ...
WAYNE SHAW NULL  NULL   NULL   NULL   NULL    

                       DATE_CY2 MonthlyGP_CY2 DATE_PY2  MonthlyGP_PY2 YOYGPDiff_M2 ...
                       10-01-2013 2770.060000  10-01-2012  NULL   -2770.060000

                       DATE_CY1 MonthlyGP_CY1 DATE_PY1  MonthlyGP_PY1 YOYGPDiff_M1  ...
SANDY LIN       NULL  NULL   NULL   NULL   NULL    
   
                       DATE_CY2 MonthlyGP_CY2 DATE_PY2  MonthlyGP_PY2 YOYGPDiff_M2  ...
                       10-01-2013 29281.200000 10-01-2012  28025.810000 1255.39

The source table is structured thusly:

I apologize for the formatting, the entry box adds quite a few extra lines.

CREATE TABLE GPByMonth (
[SPRSNSLN] [VARCHAR](61) NOT NULL,
[M] [INT] NOT NULL,
[Y] [INT] NOT NULL,
[MonthlyGP] [Decimal] (14,6) NULL,
[MonthlyRev] [Decimal] (14,6) NULL
)

Sample Data:

INSERT INTO GPByMonth VALUES ( 'BOB BELL',11,2011,11254.240000,56641.225000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',12,2011,17046.595000,98529.275000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',1,2012,13459.840000,68279.770000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',2,2012,18464.210000,96150.125000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',3,2012,20844.970000,87311.985000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',4,2012,20488.115000,91044.300000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',5,2012,16773.735000,93072.740000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',6,2012,15490.645000,78624.820000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',7,2012,17020.980000,85416.720000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',8,2012,24843.085000,116591.805000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',9,2012,13230.865000,66873.425000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',10,2012,31884.945000,147226.085000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',11,2012,10969.280000,58353.880000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',12,2012,15020.235000,74394.460000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',1,2013,18598.945000,99750.470000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',2,2013,21347.365000,84373.895000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',3,2013,19124.885000,84118.720000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',4,2013,19644.570000,105655.665000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',5,2013,18845.560000,117610.330000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',6,2013,26348.610000,115589.650000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',7,2013,11888.070000,68834.730000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',8,2013,18728.560000,108363.950000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',9,2013,20630.130000,85104.330000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',10,2013,30607.950000,141205.220000)
 INSERT INTO GPByMonth VALUES ( 'BOB BELL',11,2013,1333.650000,7149.930000)
 INSERT INTO GPByMonth VALUES ( 'WAYNE SHAW',2,2013,0.000000,0.000000)
 INSERT INTO GPByMonth VALUES ( 'WAYNE SHAW',3,2013,148.180000,868.500000)
 INSERT INTO GPByMonth VALUES ( 'WAYNE SHAW',4,2013,431.990000,1270.660000)
 INSERT INTO GPByMonth VALUES ( 'WAYNE SHAW',5,2013,1858.620000,7467.040000)
 INSERT INTO GPByMonth VALUES ( 'WAYNE SHAW',6,2013,2358.530000,6401.610000)
 INSERT INTO GPByMonth VALUES ( 'WAYNE SHAW',7,2013,2845.230000,9904.800000)
 INSERT INTO GPByMonth VALUES ( 'WAYNE SHAW',8,2013,3245.360000,11958.480000)
 INSERT INTO GPByMonth VALUES ( 'WAYNE SHAW',9,2013,1252.880000,4136.580000)
 INSERT INTO GPByMonth VALUES ( 'WAYNE SHAW',10,2013,2770.060000,9339.140000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',5,2012,27553.890000,102369.560000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',6,2012,31116.780000,114532.380000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',7,2012,22435.350000,84591.400000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',8,2012,37782.180000,127224.000000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',9,2012,26547.040000,82395.190000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',10,2012,28025.810000,96268.220000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',11,2012,20203.100000,68700.390000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',12,2012,23061.990000,82747.650000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',1,2013,26721.780000,94176.800000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',2,2013,24661.950000,82696.610000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',3,2013,29719.660000,99566.070000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',4,2013,28880.650000,93288.850000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',5,2013,26343.260000,85788.420000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',6,2013,25067.400000,84821.840000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',7,2013,24196.470000,82984.570000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',8,2013,27940.950000,97075.450000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',9,2013,26307.840000,98757.000000)
 INSERT INTO GPByMonth VALUES ( 'SANDY LIN',10,2013,29281.200000,105184.650000)


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>