我有一組數據,在名爲BWHourlyReadings
表,例如:如何組合/合併來自兩個SQL查詢結果的列?
ServiceID Hour InputOctets OutputOctets
========= ==== =========== =================
27222 1 383088 804249
27222 2 270529 688683
27222 3 247251 290124
... up to 24 hours of data
27222 24 236053 239165
28900 1 883011 914249
28900 3 444251 891124
... up to 24 hours of data
28900 24 123053 452165
對於每天有高達24個ServiceID
讀數。
我得儘可能兩個獨立PIVOT
查詢,一個用於InputOctets
列,一個用於OutputOctets
列(這裏只有一個顯示爲簡潔起見):
-- Replace HourXIn with HourXOut for OutputOctets
SELECT ServiceID, [1] AS 'Hour1In', [2] AS 'Hour2In', [3] AS 'Hour3In', ...
FROM
(
SELECT
ServiceID,
Hour,
TotalInputOctets -- Other query has OutputOctets here instead
FROM
BWHourlyReadings
) AS bw
PIVOT
(
MAX(TotalInputOctets) -- Other query has OutputOctets here instead
FOR [Hour] IN ([1], [2], [3], ... [24])
) AS pvt
這給了我,我的InputOctets
並在兩個OutputOctets
單獨的結果集,例如:
上InputOctets
的PIVOT
查詢結果:
ServiceID Hour1In Hour2In Hour3In . Hour24In
========= ======= ======= ======= ========
27222 383088 270529 247251 236053
28900 883011 0 444251 123053
上OutputOctets
的PIVOT
查詢結果:
ServiceID Hour1Out Hour2Out Hour3Out .. Hour24Out
========= ======== ======== ======== ========
27222 804249 688683 290124 239165
28900 914249 0 891124 452165
我需要製作一個像這樣的報告:
ServiceID Hour1In Hour1Out Hour2In Hour2Out Hour3In Hour3Out .. Hour24In Hour24Out
========= ======= ======== ======= ======== ======= ======== ======= ========
27222 383088 804249 270529 688683 247251 290124 236053 239165
28900 883011 914249 0 0 444251 891124 123053 452165
如何合併兩個查詢結果,產生上述報告嗎?
更新:
我已經更新的數據所需的報告格式源表示例中的數據相匹配。我對這種混亂表示歉意。
這將只是給我一個綜合作用的結果,一前一後。我需要在結果中合併兩個列集合。 – Kev 2009-06-29 11:44:39