0
我已在用下面的結構如下:逆透視行值到multple列
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)
我想這樣它看起來像到了unpivot此表:
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列,但我沒有能夠獲得倫敦和新加坡的專欄。我已經修改了下面的代碼來添加倫敦和新加坡,但一直沒有成功。我只是保持不透明嗎?
select Day, ProductName, PriceType, NewYork
from (select * from Prices) as t
Unpivot
(
NewYork for PriceType in (RegPrice1, SalePrice1, SalePrice2)
) As unpvt
感謝您的快速回復。我不認爲這是我正在尋找的。我想將所有價格類型堆疊在一列中。請參閱上面的結果表(我用完整的結果表更新了它)。 – codingknob 2014-09-18 18:30:45
更新了它,但仍然認爲有更好的方法,我們可以首先unpivot然後需要樞軸 – radar 2014-09-18 18:40:55
更新與更好的版本 – radar 2014-09-18 18:54:22