Pivot SQL table (Rows into Columns)

589 views Asked by At

I am currently returning data into a table in my SQL stored procedure. I am trying to pivot the rows into columns, and the columns into rows, but I am struggling to do so as a lot of the tutorials I am following to do this have laid out they're tables differently.

This is the select at the bottom of my stored procedure:

select 

(CASE WHEN [Fitter] IS NULL THEN (Select Distinct substring([First Name],1,1)+' '+[Second Name] from Fitters where [Fitter Id]=FitterId) ELSE Fitter END) AS Fitter,
sum([Install Sell]) as [Install Sell],
sum([Install Cost]) as [Install Cost],
sum([Install Cost Amt]) as Gross,
(select cast(CAST((TaxStatus/100 * sum([Install Cost Amt])) as decimal(18,5)) as float)) as CIS,
(select cast(CAST((VatStatus/100 * sum([Install Cost Amt])) as decimal(18,5)) as float)) as VAT,
sum([Install Cost Amt]) - (select cast(CAST((TaxStatus/100 * sum([Install Cost Amt])) as decimal(18,5)) as float)) + (select cast(CAST((VatStatus/100 * sum([Install Cost Amt])) as decimal(18,5)) as float)) as FittersPay,
sum([Install Cost Amt]) + (select cast(CAST((VatStatus/100 * sum([Install Cost Amt])) as decimal(18,5)) as float)) as Datafile

from @TempTable
group by FitterId, Fitter, TaxStatus, VatStatus

This is the data it returns: enter image description here

I would like for the columns to pivot as rows and the rows to pivot as columns...

I have searched around online and am struggling to figure this out, i was wondering if i could get some assistance with this please.

Any assistance is appreciated.

1

There are 1 answers

0
Giorgi Nakeuri On BEST ANSWER

This is done by first unpivoting and then pivoting. Here is an example, that you can adjust yo your data:

DECLARE @t TABLE(col0 VARCHAR(20), col1 MONEY, col2 MONEY, col3 MONEY)

INSERT INTO @t VALUES
('aaaaaa', 1, 1.2, 0),
('bbbbbb', 2, 2.2, 0),
('cccccc', 3, 3.3, 100),
('dddddd', 4, 4.4, 0)

SELECT * FROM @t 

SELECT * FROM @t
UNPIVOT (a FOR b IN([col1],[col2],[col3])) up
PIVOT (MAX(a) FOR col0 IN([aaaaaa],[bbbbbb],[cccccc],[dddddd])) p

Output1:

col0    col1    col2    col3
aaaaaa  1.00    1.20    0.00
bbbbbb  2.00    2.20    0.00
cccccc  3.00    3.30    100.00
dddddd  4.00    4.40    0.00

Output2:

b       aaaaaa  bbbbbb  cccccc  dddddd
col1    1.00    2.00    3.00    4.00
col2    1.20    2.20    3.30    4.40
col3    0.00    0.00    100.00  0.00

It depends on type of your data, but you could need to do it dynamically. There is plenty of examples on the site. Just search for dynamic pivoting.

EDIT:

Something like this:

select * from (
select 

(CASE WHEN [Fitter] IS NULL THEN (Select Distinct substring([First Name],1,1)+' '+[Second Name] from Fitters where [Fitter Id]=FitterId) ELSE Fitter END) AS Fitter,
sum([Install Sell]) as [Install Sell],
sum([Install Cost]) as [Install Cost],
sum([Install Cost Amt]) as Gross,
(select cast(CAST((TaxStatus/100 * sum([Install Cost Amt])) as decimal(18,5)) as float)) as CIS,
(select cast(CAST((VatStatus/100 * sum([Install Cost Amt])) as decimal(18,5)) as float)) as VAT,
sum([Install Cost Amt]) - (select cast(CAST((TaxStatus/100 * sum([Install Cost Amt])) as decimal(18,5)) as float)) + (select cast(CAST((VatStatus/100 * sum([Install Cost Amt])) as decimal(18,5)) as float)) as FittersPay,
sum([Install Cost Amt]) + (select cast(CAST((VatStatus/100 * sum([Install Cost Amt])) as decimal(18,5)) as float)) as Datafile

from @TempTable
group by FitterId, Fitter, TaxStatus, VatStatus) t

UNPIVOT (a FOR b IN([Install Sell],[Install Cost],[Gross]/*,...*/)) up
PIVOT (MAX(a) FOR Fitter IN([D Page],[J Hopley]/*,...*/)) p