我有一张如下表:
| Email | Category | Amount | Post_date |
| 123@234 | ATM | 20 | 2016-09-20 |
| 234@345 | POS | 30 | 2016-09-21 |
| 345@567 | FOOD | 40 | 2016-09-22 |
| 567@789 | ENTE | 50 | 2016-09-23 |
我希望输出看起来像这样:
| Email | Category | Last 1 day| Last 3 days | last 5 days |
| 123@234 | ATM amount | 20 | 40 | 50 |
| | POS amount | 0 | 30 | 40 |
| | ATM count | 2 | 3 | 3 |
| | POS count | 0 | 0 | 3 |
我可以编写一个查询来同时生成一个列,例如最近1天,最后3天等。
我的问题是:
答案 0 :(得分:0)
我强烈怀疑你想要这样的查询:
select email, category,
sum(case when post_date >= current_date - interval '1 day' then 1 else 0 end) as last_1,
sum(case when post_date >= current_date - interval '3 day' then 1 else 0 end) as last_3,
sum(case when post_date >= current_date - interval '5 day' then 1 else 0 end) as last_5
from t
group by email, category;
我承认这是推测,因为问题不明确。