简体   繁体   中英

Combining multiple SQL queries of same type in a single result set in PostgreSQL

I am new to SQL so pardon me for this naive question.

I have a date range say '20180903 - 20180905' so 3 days from 3rd Sep to 5th Sep .

Now for each day we have 24 military hours ranging from 0-23 and we write a query for a range of 4 at a time in a single day .

So, suppose,

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

Now I have taken for 20180903 military hour range 0-3 . and it returns a row like

46 | 224 | 208 | 16

Should I have to write the same query five more times with ranges like 4-7, 8-11, 12- 15, 16-19, 20 - 23 ? and then aggregate and return the query set with 6 rows?

How can I write it in better way?

Also, it's only for 20180903 .

How to do if for the other 2 days and return all 18 rows each(6 each day) ?

eg

the resultset might look like

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

Use conditional aggregation and group by . Something like this:

select end_date, 
       (floor(td2.military_hour / 4) * 4) as military_hour,
       count(w1.wk_occurance_id) as total_occurances,
       sum( (w1.status = 'SUCCEEDED')::int ) as SUCCEEDED_instances, 
       sum( (w1.status <> 'SUCCEEDED')::int ) as FAILED_instances  
from work_instances w1 inner join  
     time_table td2 
     on   w1.end_time = td2.time_id
where end_date >= 20180901' and end_date <2018
group by end_date, (floor(td2.military_hour / 4) * 4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM