2017-04-26 63 views
0

我對相對複雜的SQL相當陌生,可以對問題使用一些幫助。我有一個查詢:如何獲得記錄計數的總和(來自先前的查詢)

select 
    case 
    when group_id = 'ABC Acme' then 'ABC' 
    when group_id like 'Premium%' then 'PREM' 
    when group_id like '%Marvel%' then 'MRV' 
    else NULL 
    end client, 
    load_date, report_date, count(*) as record_count 
from tablename 
group by client, load_date, report_date 
order by client, load_date; 

返回以下內容:

client load_date report_date record_count 
ABC  4/1/2016 2/28/2016 16108 
PREM 4/19/2016 3/31/2017 5348 
MRV  4/19/2016 3/31/2017 8335 

我想,如果可能的話,得到的總和record_count(或29791)的。理想情況下,在上面的結果中添加另一行將會很好。我嘗試以下查詢,但沒有奏效...

select 
    case 
    when group_id = 'ABC Studios' then 'ABC' 
    when group_id like 'Premium%' then 'PREM' 
    when group_id like '%Marvel%' then 'MRV' 
    else NULL 
    end client, 
    load_date, report_date, count (*) 
from tablename 
    group by client, load_date, report_date 
    union all 
select 'SUM', count(*) 
from tablename; 

我得到以下錯誤:

An error occurred when executing the SQL command: 
--get summary ttl of counts by load_date for each client 
select 
case wh... 
[Amazon](500310) Invalid operation: each UNION query must have the same number of columns; 
Execution time: 0s 
1 statement failed. 
+0

的總記錄添加額外的列中的錯誤是顯而易見的。如果你想'UNION'兩個查詢都需要有相同數量的列。你應該在你的問題中包括什麼是你的預期結果,所以我們可以幫助你建立查詢。 –

+0

你是對的@JuanCarlosOropeza。我確實得到了明確的答案,幫助我解決了這個問題。 THNXS用於跳躍和幫助,非常感謝! – Gar

回答

0

大多數數據庫支持grouping setsrollup,這絕對簡化了這一點。

可維護性和一致性,我更喜歡使用一個CTE:

with t as (
     select (case when group_id = 'ABC Acme' then 'ABC' 
        when group_id like 'Premium%' then 'PREM' 
        when group_id like '%Marvel%' then 'MRV' 
       end) as client, 
      load_date, report_date, count(*) as record_count 
     from tablename 
     group by client, load_date, report_date 
    ) 
select client, load_date, report_date, record_count 
from ((select t.*, 1 as priority 
     from t 
    ) union all 
     (select 'SUM', NULL, NULL, sum(record_count), 2 
     from t 
    ) 
    ) t 
order by priority, client, load_date; 

注:

  • 通過將在CTE任何潛在的過濾條件或計算,可以確保一致性。
  • 實際上,數據庫按順序返回UNION ALL的行。但這並不能保證。明確使用priority列可確保數據的順序正確。
  • 添加附加子集相當容易。
  • grouping sets是非常優選的。
+0

您的答案是戈登100%!我想我需要更多地關注CTE。我特別喜歡你添加「優先級」的用法來正確排序結果。我可以建立在這個答案上,也許讓我的查詢更復雜一點 – Gar

0

當你union記錄集在一起,他們需要有相同的列數。現在,您的第一個查詢有4列,第二個查詢只有2列。只需在load_date中選擇null,然後在第二個查詢中選擇report_date(無論如何,這些字段對於總計都沒有意義)。

select 
    case 
    when group_id = 'ABC Studios' then 'ABC' 
    when group_id like 'Premium%' then 'PREM' 
    when group_id like '%Marvel%' then 'MRV' 
    else 
    NULL 
    end client, load_date, report_date, count (*), 1 priority 
    from tablename 
    group by client, load_date, report_date 

    union all 

    select 'SUM', null, null, count(*), 2 priority 
    from tablename 
    order by priority; 
+0

嗨@Jerrad,你的回答也很好,但我需要添加一個小修改,以便正確排序結果(使用「優先級」排序)。現在,每當我運行您的解決方案時,我都會看到彙總行出現在不同rowid – Gar

+0

上,足以解決問題。只需在SELECT中添加一個優先級列。給它的實際數據值爲1,總數爲2。然後按照該優先級進行排序,將總行放在底部。 – Jerrad

+0

它確實如此,THNXS再次!這也是一個非常優雅/簡單的解決方案。我可以稍微玩一下,並將其擴展到更宏大的場景 – Gar

0

可以作爲

declare @ttl int; 
Set @ttl = (Select Count(*) from tablename) 
select 
    case 
    when group_id = 'ABC Studios' then 'ABC' 
    when group_id like 'Premium%' then 'PREM' 
    when group_id like '%Marvel%' then 'MRV' 
    else 
    NULL 
    end client, load_date, report_date, count (*), @ttl AS Sum 
    from tablename 
    group by client, load_date, report_date 
+0

@Jerrad:Rahul,這個解決方案並不適合我(完全),但我認爲這完全是因爲我使用Redshift,並且顯然聲明並使用變量像拔牙一樣。但我喜歡這種技術,我想我可以將它融入到一個存儲過程中 – Gar