gpt4 book ai didi

mysql - 因帕拉/SQL : Can I have different time-period for each group?

转载 作者:行者123 更新时间:2023-11-29 11:18:14 26 4
gpt4 key购买 nike

我有下表:

id   | timestamp  |  team  
----------------------------
1 | 2016-05-06 | A
2 | 2016-03-02 | A
3 | 2015-12-01 | A
4 | 2016-07-05 | B
5 | 2016-06-30 | B
6 | 2016-06-28 | B
7 | 2016-04-05 | C
8 | 2016-04-02 | C
9 | 2016-01-02 | C

我想按团队分组并找到每个团队的最后时间戳,所以我这样做了:

select team, max(timestamp) from my_table group by team

到目前为止一切正常。但是,现在我想知道每个团队上个月有多少个不同的 id。例如,对于A队来说,从2016-04-07到2016-05-06,所以这个计数是1。对于B队来说,最后一个月是从2016-06-06到2016-07-05,所以计数是 3。对于 C 团队,最后一个月是从 2016-03-06 到 2016-04-05,计数是 2。我的预期输出应该如下所示:

team  | max(timestamp)  |  count_in_last_month
------------------------------------------------
A | 2016-05-06 | 1
B | 2016-07-05 | 3
C | 2016-04-05 | 2

这可以使用 Impala 查询导出吗?谢谢!

最佳答案

将原始表与获取最大时间戳的子查询连接起来。

SELECT t1.team, t2.month_end, COUNT(DISTINCT t1.id) AS count_in_last_month
FROM my_table AS t1
JOIN (SELECT team, MAX(timestamp) AS month_end
FROM my_table
GROUP BY team) AS t2
ON t1.team = t2.team
AND t1.timestamp BETWEEN DATE_SUB(month_end, INTERVAL 1 MONTH) AND month_end
GROUP BY t1.team, t2.month_end

DEMO

关于mysql - 因帕拉/SQL : Can I have different time-period for each group?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39397039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com