繁体   English   中英

在 PostgreSQL 中的单个结果集中组合多个相同类型的 SQL 查询

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

我是 SQL 的新手,所以请原谅我这个幼稚的问题。

我有一个日期范围说 '20180903 - 20180905' 所以从3rd Sep to 5th Sep 3 天。

现在每天我们有 24 个军事小时,范围从0-23 ,我们a range of 4 at a time in a single day编写a range of 4 at a time in a single day的查询。

所以,假设,

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

现在我采用了20180903军事小时范围0-3 它返回一行

46 | 224 | 208 | 16

我是否应该再写五次相同的查询,范围是4-7, 8-11, 12- 15, 16-19, 20 - 23 然后聚合并返回具有 6 行的查询集?

我怎样才能以更好的方式编写它?

此外,它仅适用于20180903

如果在其他2 days and return all 18 rows each(6 each day)怎么办?

例如

结果集可能看起来像

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

使用条件聚合和group by 像这样的东西:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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