Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I combine/merge columns from two SQL query results?
    primarykey
    data
    text
    <p>I have a set of data in a table named <code>BWHourlyReadings</code>, for example:</p> <pre><code>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 </code></pre> <p>For each day there are up to 24 readings per <code>ServiceID</code>.</p> <p>I've got as far as <strong>two separate</strong> <code>PIVOT</code> queries, one for the <code>InputOctets</code> column and one for the <code>OutputOctets</code> column (only one shown here for brevity):</p> <pre><code>-- 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 </code></pre> <p>This gives me my <code>InputOctets</code> and <code>OutputOctets</code> in two separate result sets, for example:</p> <p>The <code>PIVOT</code> query result on <code>InputOctets</code>:</p> <pre><code>ServiceID Hour1In Hour2In Hour3In . Hour24In ========= ======= ======= ======= ======== 27222 383088 270529 247251 236053 28900 883011 0 444251 123053 </code></pre> <p>The <code>PIVOT</code> query result on <code>OutputOctets</code>:</p> <pre><code>ServiceID Hour1Out Hour2Out Hour3Out .. Hour24Out ========= ======== ======== ======== ======== 27222 804249 688683 290124 239165 28900 914249 0 891124 452165 </code></pre> <p>I need to produce a report like this:</p> <pre><code>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 </code></pre> <p>How do I merge the two query results to produce the report above?</p> <p><strong>Update:</strong></p> <p>I've updated the data in the desired report format to match the data in the source table example. My apologies for the confusion.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload