title: Combining multiple SQL queries of same type in a single result set in PostgreSQL
body: <p>I am new to SQL so pardon me for this naive question.</p>
  
  <p>I have a date range say '20180903 - 20180905' so 3 days from <code>3rd Sep to 5th Sep</code>.</p>
  
  <p>Now for each day we have 24 military hours ranging from <code>0-23</code> and we write a query for <code>a range of 4 at a time in a single day</code>.</p>
  
  <p>So, suppose,</p>
  
  <pre><code>select 
  COUNT(wk_id),
  SUM(total_occurances) as total_wk_occurances,
  SUM(SUCCEEDED_instances) as total_successful_occurances,
  SUM(FAILED_instances) as total_error_occurances 
  from
  (
      select w1.wk_id,
      COUNT(w1.wk_occurance_id) as total_occurances,
      sum(case when w1.status='SUCCEEDED' then 1 else 0 end ) as SUCCEEDED_instances, 
      sum(case when w1.status !='SUCCEEDED' then 1 else 0 end ) as FAILED_instances  
      from 
          work_instances w1 
      inner join  
          time_table td2 
     on  
          w1.end_time = td2.time_id
     where  
          (td2.military_hour between 0 and 3 and end_date='20180903') group by w1.wf_id
  ) as sub_q1
  </code></pre>
  
  <p>Now I have taken for <code>20180903</code> military hour range <code>0-3</code>. and it returns a row like</p>
  
  <pre><code>46 | 224 | 208 | 16
  </code></pre>
  
  <p>Should I have to write the same query five more times with ranges like <code>4-7, 8-11, 12- 15, 16-19, 20 - 23</code>? and then aggregate and return the query set with 6 rows?</p>
  
  <p><strong>How can I write it in better way?</strong></p>
  
  <p>Also, it's only for <code>20180903</code>.</p>
  
  <p>How to do if for the other <code>2 days and return all 18 rows each(6 each day)</code>?</p>
  
  <p>e.g.</p>
  
  <p>the resultset might look like</p>
  
  <pre><code>COUNT   |   total_wk_occurances  |  total_succeessful_occurances  |  total_error_Occurances
  
  46      |   224                  |  208                          | 16
  34      |   100                  |  75                           | 25
  46      |   224                  |  208                          | 16
  34      |   100                  |  75                           | 25
  46      |   224                  |  208                          | 16
  34      |   100                  |  75                           | 25
  46      |   224                  |  208                          | 16
  34      |   100                  |  75                           | 25
  46      |   224                  |  208                          | 16
  34      |   100                  |  75                           | 25
  46      |   224                  |  208                          | 16
  34      |   100                  |  75                           | 25
  46      |   224                  |  208                          | 16
  34      |   100                  |  75                           | 25
  46      |   224                  |  208                          | 16
  34      |   100                  |  75                           | 25
  46      |   224                  |  208                          | 16
  34      |   100                  |  75                           | 25
  </code></pre>