2012-03-23 103 views
2

現狀轉換成表格與BI

date (nvarchar(9)) 
sku  (nvarchar(5)) 
smith (decimal) 
jones (decimal) 
jonson (decimal) 
nguyen (decimal) 



date   sku smith  jones  johnson  nguyen 
----------------------------------------------------------- 
11/4/2007 X2271  2404 9055,33  7055,22   0 
11/4/2007 B1112 108,99   0  244,92 1001.01 

要求的結果:

date   sku  salesperson sales 
------------------------------------------ 
11/4/2007 X2271  Smith   2404 
11/4/2007 X2271  Jones  9055,33 
11/4/2007 X2271  Johnson  7055,22 
11/4/2007 B1112  Smith   108,99 
11/4/2007 B1112  Johnson  244,92 
11/4/2007 B1112  Nguyen  1001,01 

會需要一些幫助來解決這個問題。此任務與BI任務相關。

回答

2

可以使用UNPIVOT命令來實現這一點。

SELECT 
    date, 
    sku, 
    salesperson, 
    sales 
FROM (
    SELECT 
     date, 
     sku, 
     smith, 
     jones, 
     johnson, 
     nguyen 
    FROM 
     YourTable 
) q 
UNPIVOT 
(sales FOR salesperson in (smith, jones, johnson, nguyen)) AS YourUnpivot