我有一個查詢,給出的結果是一些屬性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
我一直在試圖與選擇的情況下,但我不能讓結構工程
是否存在有限且固定數量的類別? –
[踢壞的習慣:使用舊式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'語法它的使用是不鼓勵 –