按子查询选择,分组

时间:2017-12-09 17:08:01

标签: sql postgresql

以下查询适用于为数量> 0的任何行计算不同的UNIQUE_MEM_ID。我想更改此查询的逻辑以捕获具有总和(金额)>的不同UNIQUE_MEM_ID。 100个一个月

从概念上讲,改变逻辑的数量>任何一行100,总和(金额)>一个月内任何用户都可以使用100个。

select to_char(optimized_transaction_date, 'YYYY-MM') as month, cobrand_id as cobrand,
   count(distinct UNIQUE_MEM_ID) as distinct_count
into temp_09.z_members
from yi_fourmpanel.card_panel
and amount >0
group by  to_char(optimized_transaction_date, 'YYYY-MM'), cobrand;

1 个答案:

答案 0 :(得分:0)

使用having子句:

select to_char(optimized_transaction_date, 'YYYY-MM') as month,
       cobrand_id as cobrand,
       count(distinct UNIQUE_MEM_ID) as distinct_count
into temp_09.z_members
from yi_fourmpanel.card_panel
group by to_char(optimized_transaction_date, 'YYYY-MM'), cobrand
having sum(amount) > 100;