0

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.

4
  • Which dbms? (Doesn't look like ANSI SQL...) Commented Jun 11, 2015 at 8:38
  • I am using Microsoft SQL server @jarlh Commented Jun 11, 2015 at 8:40
  • Show sample data and desired output Commented Jun 11, 2015 at 8:44
  • I have edited my question, I hope that helps @PravinDeshmukh Commented Jun 11, 2015 at 8:51

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.