【发布时间】:2014-09-18 17:59:34
【问题描述】:
我有以下结构:
http://sqlfiddle.com/#!6/0e72e/8
CREATE TABLE Prices
(
Day date,
ProductName nVARCHAR(10),
Location nVARCHAR(10),
RegPrice1 float,
SalePrice1 float,
SalePrice2 float
)
INSERT INTO Prices VALUES ('6/24/2014', 'xbox', 'NewYork',30,20,10)
INSERT INTO Prices VALUES ('6/24/2014', 'xbox', 'London', 100,80,60)
INSERT INTO Prices VALUES ('6/24/2014', 'xbox', 'Singapore', 70,50,30)
INSERT INTO Prices VALUES ('6/24/2014', 'watch1','NewYork', 500,400,300)
INSERT INTO Prices VALUES ('6/24/2014', 'watch1','London', 1000,800,600)
INSERT INTO Prices VALUES ('6/24/2014', 'watch1','Singapore', 999,888,777)
我想取消透视此表,使其看起来像:
Day Pr_Name PriceType NewYork London Singapore 2014-06-24 xbox RegPrice1 30 100 70 2014-06-24 xbox SalePrice1 20 80 50 2014-06-24 xbox SalePrice2 10 60 30 2014-06-24 watch1 RegPrice1 500 1000 999 2014-06-24 watch1 SalePrice1 400 800 888 2014-06-24 watch1 SalePrice1 300 600 777
我能够取消旋转一层来获得 NewYork 列,但我无法获得 London 和 Singapore 列。我已经修改了下面的代码以添加伦敦和新加坡,但没有成功。我只是继续不旋转吗?
select Day, ProductName, PriceType, NewYork
from (select * from Prices ) as t
Unpivot
(
NewYork for PriceType in (RegPrice1, SalePrice1, SalePrice2)
) As unpvt
【问题讨论】: