impala/sql:我可以为每个组设置不同的时间段吗?

7bsow1i6  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(389)

我有下表:

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查询来派生?谢谢!

vuktfyat

vuktfyat1#

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

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

演示

相关问题