Sql 每周为每位客户获取最新记录
我试图查询一个表并返回按周分组的客户选择的最新组合。例如,以下是表格:Sql 每周为每位客户获取最新记录,sql,impala,Sql,Impala,我试图查询一个表并返回按周分组的客户选择的最新组合。例如,以下是表格: (int) (string) (int) (bigint) Customer ID Choice Week Inserted at 100 a, b, c 2 20171002 100 a, b 2 20171004 101 b, c, d 2 2017100
(int) (string) (int) (bigint)
Customer ID Choice Week Inserted at
100 a, b, c 2 20171002
100 a, b 2 20171004
101 b, c, d 2 20171002
102 a, c, d 2 20171002
103 a, b, c 2 20171002
103 a, b, d 2 20171003
100 a, b, c, d 3 20171010
101 a, c, d 3 20171010
101 b, c, d 3 20171011
102 a, b 3 20171010
103 a, b, c 3 20171010
103 b, c, d 3 20171012
103 a, b, d 3 20171014
这就是我想提出的问题:
Customer ID Choice Week Inserted at
100 a, b 2 20171004
101 b, c, d 2 20171002
102 a, c, d 2 20171002
103 a, b, d 2 20171003
100 a, b, c, d 3 20171010
101 b, c, d 3 20171011
102 a, b 3 20171010
103 a, b, d 3 20171014
客户每天只能对其选择进行一次更改,因此我不必担心客户在一天内进行多次更改
这是我开始的内容,但它缺少很多行。有什么反馈吗
SELECT c.customer, c.combo, c.week, c.date
FROM tableCombos AS c
WHERE not exists (SELECT *
FROM tableCombos AS recent
WHERE recent.customer = c.customer
AND recent.date > c.date)
使用窗口功能:
select tc.*
from (select tc.*,
row_number() over (partition by customer, week order by date desc) as seqnum
from tableCombos tc
) tc
where seqnum = 1;
首先,我希望获得按周和客户分组的最大o日期:
select customerId , week, max(insertedAt) as date
group by customerId , WEEK
之后,我可以将这个数据集与主表连接起来
SELECT c.customer, c.combo, c.week, c.date
FROM tableCombos AS c
join (select customerId , week, max(insertedAt) as date
group by customerId , WEEK) X
on c.customerId = X.customerId
and c.week = X.week
and c.date = X.date
你在使用哪种RDBMS?我在Cloudera Impala工作