2014-12-30 48 views
1

由於大量數據涌入了幾張表,因此我有一個數據庫大約有50個表(現在),因此我的任務是創建一個小時彙總的數據並將其轉儲到另一個表中。因此,運行關於原始數據的報告花費相當長的時間,因爲新數據庫(2周齡)已經在我提取數據的兩個表格之一中達到了20萬條記錄。在Postgres中將數據組數據插入彙總表

該查詢爲客戶提供三種可能的結果 - 「cust1」,「cust2」和「cust3」,每種都有一個選定的卡片(郵件回覆產品質量問卷和潛在獲獎),這是13種選擇之一。字母表示(「A」王牌,「K」特大等的相關聯的值)

這裏是子查詢中的一個和相應的結果:

select sp_cust_card_sequence(cards.cust1) as seq, cards.cust1 as card, count(cards.cust1) as card_count, rtt.game_id as game_id, gt.promo_id as promo_id, gt.choice_id as choice_id, extract(hour from header.start_timestamp) as hour, header.start_timestamp::timestamp::date as date 
    from game_bac_cards cards 
    inner join card_cust_resp header ON (header.id = cards.game_id) 
    inner join game_table gt ON (header.promo_id = gt.promo_id) 
    inner join ref_table_type rtt ON (gt.table_type_id = rtt.id) 
    where result <> 'undef' 
    group by date, hour, card, rtt.game_id, gt.promo_id, gt.choice_id, cards.cust1 

And the result:

本質上,我想組隊例如,按小時從卡列中的「K」的所有值。與下面的查詢,我似乎能夠幾乎做到這一點,但下面的代碼片段顯示,「ķ」爲小時「」上「2014年12月4日」的價值有兩個條目,而不僅僅是一個。我確信有這樣一個更優雅的方式。

最終查詢:

select date, hour, card, seq, sum(card_count) as card_count, game_id, choice_id, promo_id 
from (
select date, hour, card, seq, sum(card_count) as card_count, game_id, choice_id, promo_id from (
    select sp_cust_card_sequence(cards.cust1) as seq, cards.cust1 as card, count(cards.cust1) as card_count, rtt.game_id as game_id, gt.promo_id as promo_id, gt.choice_id as choice_id, extract(hour from header.start_timestamp) as hour, header.start_timestamp::timestamp::date as date 
    from game_bac_cards cards 
    inner join card_cust_resp header ON (header.id = cards.game_id) 
    inner join game_table gt ON (header.promo_id = gt.promo_id) 
    inner join ref_table_type rtt ON (gt.table_type_id = rtt.id) 
    where result <> 'undef' 
    group by date, hour, card, rtt.game_id, gt.promo_id, gt.choice_id, cards.cust1 
) as cust1_table 
where cust1_table.card is not null and cust1_table.card <> '' 
group by date, hour, card_count, card, seq, game_id, choice_id, promo_id 

union all 

select date, hour, card, seq, sum(card_count) as card_count, game_id, choice_id, promo_id from (
    select sp_cust_card_sequence(cards.cust2) as seq, cards.cust2 as card, count(cards.cust2) as card_count, rtt.game_id as game_id, gt.promo_id as promo_id, gt.choice_id as choice_id, extract(hour from header.start_timestamp) as hour, header.start_timestamp::timestamp::date as date 
    from game_bac_cards cards 
    inner join card_cust_resp header ON (header.id = cards.game_id) 
    inner join game_table gt ON (header.promo_id = gt.promo_id) 
    inner join ref_table_type rtt ON (gt.table_type_id = rtt.id) 
    where result <> 'undef' 
    group by date, hour, card, rtt.game_id, gt.promo_id, gt.choice_id, cards.cust2 
) as cust2_table 
where cust2_table.card is not null and cust2_table.card <> '' 
group by date, hour, card_count, card, seq, game_id, choice_id, promo_id 

union all 

select date, hour, card, seq, sum(card_count) as card_count, game_id, choice_id, promo_id from (
    select sp_cust_card_sequence(cards.cust3) as seq, cards.cust3 as card, count(cards.cust3) as card_count, rtt.game_id as game_id, gt.promo_id as promo_id, gt.choice_id as choice_id, extract(hour from header.start_timestamp) as hour, header.start_timestamp::timestamp::date as date 
    from game_bac_cards cards 
    inner join card_cust_resp header ON (header.id = cards.game_id) 
    inner join game_table gt ON (header.promo_id = gt.promo_id) 
    inner join ref_table_type rtt ON (gt.table_type_id = rtt.id) 
    where result <> 'undef' 
    group by date, hour, card, rtt.game_id, gt.promo_id, gt.choice_id, cards.cust3 
) as cust3_table 
where cust3_table.card is not null and cust3_table.card <> '' 
group by date, hour, card_count, card, seq, game_id, choice_id, promo_id 
) as card_details 
and card_details.card is not null and card_details.card <> '' 
group by date, hour, card, card_count, seq, game_id, choice_id, promo_id 
order by date, hour, seq 

結果只顯示卡ķ |小時 |日期12/4/2014。這應該只是一行而不是兩行。 (?)

enter image description here

任何幫助,不勝感激!

回答

1

嘗試通過日期,小時通過在card_count刪除該組中的外部查詢

組,card_count,卡,序列,game_id,choice_id,PROMO_ID

+0

良好的漁獲伊萬,奏效! :) – SiriusBits