2015-07-01 21 views
4

我有一個查詢,給出的結果是一些屬性categorizedm但我需要每一個類別變成一列打開一個類別爲列在SQL Server

這是我的查詢:

SELECT hist_statusevents.eqmt, 
     hist_exproot.shiftdate, 
     hist_statusevents.category, 
     Sum(hist_statusevents.duration/3600) as Value 
FROM Powerview.dbo.hist_eqmtlist hist_eqmtlist, 
    Powerview.dbo.hist_exproot hist_exproot, 
     Powerview.dbo.hist_statusevents hist_statusevents 
WHERE hist_exproot.shiftindex = hist_statusevents.shiftindex And 
     hist_statusevents.shiftindex = hist_eqmtlist.shiftindex And 
     hist_statusevents.eqmt = hist_eqmtlist.eqmtid And 
     hist_statusevents.eqmt like 'MOTO%' 
GROUP BY hist_statusevents.eqmt, 
     hist_exproot.shiftdate, 
     hist_statusevents.category 

這是查詢的輸出:

eqmt   shiftdate   category Value 
MOTO705   2011-01-22 00:00:00 5  13,9597222805023 
MOTO706   2011-01-28 00:00:00 3  0,280277773737907 
MOTO704   2011-02-17 00:00:00 6  8,92749977111816 
MOTO705   2011-02-09 00:00:00 6  10,07972240448 
MOTO703   2011-03-15 00:00:00 1  22,6561107933521 
MOTO704   2011-03-11 00:00:00 5  24 
MOTO706   2011-01-27 00:00:00 1  9,95361125469208 
MOTO703   2011-03-16 00:00:00 6  3,79916667938232 
MOTO704   2011-01-08 00:00:00 6  24 

但我需要得到的結果是這樣的:

eqmt   shiftdate   1 2 3 4 5 6 7 
MOTO706   2011-01-28 00:00:00 values for each category 
MOTO704   2011-02-17 00:00:00 
MOTO705   2011-02-09 00:00:00 
MOTO703   2011-03-15 00:00:00 

我一直在試圖與選擇的情況下,但我不能讓結構工程

+0

是否存在有限且固定數量的類別? –

+1

[踢壞的習慣:使用舊式JOIN](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins。 aspx) - 在ANSI - ** 92 ** SQL標準(**超過20年前的**)中,舊式*逗號分隔的表*樣式列表被替換爲* proper * ANSI'JOIN'語法它的使用是不鼓勵 –

回答

1

如果類別值是固定的,都在範圍(1,7),那麼你可以使用基於案例的聚集像下面 如果類別值是動態的,則需要使用動態sql來執行pivot

SELECT hist_statusevents.eqmt, 
     hist_exproot.shiftdate, 
     sum(case when hist_statusevents.category =1 then (hist_statusevents.duration/3600) else 0 end) as '1', 
     sum(case when hist_statusevents.category =2 then (hist_statusevents.duration/3600) else 0 end) as '2', 
     sum(case when hist_statusevents.category =3 then (hist_statusevents.duration/3600) else 0 end) as '3', 
     sum(case when hist_statusevents.category =4 then (hist_statusevents.duration/3600) else 0 end) as '4', 
     sum(case when hist_statusevents.category =5 then (hist_statusevents.duration/3600) else 0 end) as '5', 
     sum(case when hist_statusevents.category =6 then (hist_statusevents.duration/3600) else 0 end) as '6', 
     sum(case when hist_statusevents.category =7 then (hist_statusevents.duration/3600) else 0 end) as '7' 

FROM Powerview.dbo.hist_eqmtlist hist_eqmtlist, Powerview.dbo.hist_exproot hist_exproot, Powerview.dbo.hist_statusevents hist_statusevents 
WHERE hist_exproot.shiftindex = hist_statusevents.shiftindex And hist_statusevents.shiftindex = hist_eqmtlist.shiftindex And hist_statusevents.eqmt = hist_eqmtlist.eqmtid And hist_statusevents.eqmt like 'MOTO%' 
GROUP BY hist_statusevents.eqmt, hist_exproot.shiftdate 
+0

我得到這個錯誤; Msg 102,Level 15,State 1,Line 3 ')'附近的語法不正確。 – Pipebritop

+0

@Pipebritop,最終失蹤的情況下,現在嘗試 – radar

+0

偉大的!但我意識到比值didnt繼續團例如'eqmt \t \t shiftdate 1 \t \t MOTO703 \t 2011-01-01 00:00:00 \t \t 18,3538888692856 0 \t MOTO703 \t 2011-01-01 00:00:00 \t \t \t 5,64611101150513 0' – Pipebritop