I have a dataset in a table named BWHourlyReadings , for example:
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
For each day, there are up to 24 readings per ServiceID .
I have up to two simple PIVOT queries , one for the InputOctets column and one for the OutputOctets column (for brevity only):
-- 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
This gives me my InputOctets and OutputOctets in two separate result sets, for example:
The result of the PIVOT request on InputOctets :
ServiceID Hour1In Hour2In Hour3In . Hour24In
The result of the PIVOT request for OutputOctets :
ServiceID Hour1Out Hour2Out Hour3Out .. Hour24Out
I need to create a report like this:
ServiceID Hour1In Hour1Out Hour2In Hour2Out Hour3In Hour3Out .. Hour24In Hour24Out
How to combine two query results to create the report above?
Update:
I updated the data in the desired report format to match the data in the sample source table. I apologize for the confusion.