Pivot SQL table(行转列)
Pivot SQL table (Rows into Columns)
我目前正在将数据返回到 SQL 存储过程中的 table。我正在尝试将行转换为列,将列转换为行,但我正在努力这样做,因为我正在遵循的很多教程都指出它们是 tables 不同的。
这是我存储过程底部的select:
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
这是数据returns:
我希望列以行为中心,行以列为中心...
我在网上搜索了一下,正在努力解决这个问题,我想知道我是否可以得到一些帮助。
感谢任何帮助。
这是通过先取消旋转然后旋转来完成的。这是一个示例,您可以调整您的数据:
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
输出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
输出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
这取决于您的数据类型,但您可能需要动态执行。网站上有很多例子。只需搜索 dynamic pivoting
.
编辑:
像这样:
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
我目前正在将数据返回到 SQL 存储过程中的 table。我正在尝试将行转换为列,将列转换为行,但我正在努力这样做,因为我正在遵循的很多教程都指出它们是 tables 不同的。
这是我存储过程底部的select:
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
这是数据returns:
我希望列以行为中心,行以列为中心...
我在网上搜索了一下,正在努力解决这个问题,我想知道我是否可以得到一些帮助。
感谢任何帮助。
这是通过先取消旋转然后旋转来完成的。这是一个示例,您可以调整您的数据:
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
输出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
输出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
这取决于您的数据类型,但您可能需要动态执行。网站上有很多例子。只需搜索 dynamic pivoting
.
编辑:
像这样:
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