简体   繁体   English

数据透视表(行)

[英]Pivot SQL table (Rows into Columns)

I am currently returning data into a table in my SQL stored procedure. 我目前正在将数据返回到SQL存储过程中的表中。 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: 这是它返回的数据: 在此处输入图片说明

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. 任何帮助表示赞赏。

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: 输出1:

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: 输出2:

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 . 只需搜索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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM