簡體   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