SQL-仅过滤SQL中的第一条记录(Amazon Redshift)

时间:2018-07-20 13:08:26

标签: sql amazon-redshift

我有一个查询,用于从一个表中提取数据,该表存储客户数据及其反馈。但是,我遇到一个问题,即同一客户(cust_id)具有多个条目。我该如何修改它以仅返回第一行(基于时间戳)而忽略所有其他记录。

我正在使用Amazon redshift。

with q1 as 
(select cust_id,
       sum(case when response <= 6 then 1 else 0 end) as bad,
       sum(case when response between 7 and 8 then 1 else 0 end) as good
       from customers 
       group by cust_id
       order by 1 DESC ,last_visit_datetime desc),
q2 as (select cust_id,rating as neg_rating,response as neg_response from customers 
where rating is not null
order by neg_rating asc, last_visit_datetime desc )
select DISTINCT q1.cust_id,q1.good,q1.bad,q2.neg_response,q2.neg_rating
from q1 join q2 on q1.cust_id = q2.cust_id

谁能帮忙,谢谢..

1 个答案:

答案 0 :(得分:2)

使用row_number为每个cust_id获取一行,然后进行汇总。

select cust_id,
sum(case when response <= 6 then 1 else 0 end) as bad,
sum(case when response between 7 and 8 then 1 else 0 end) as good
from (select c.*,row_number() over(partition by cust_id order by last_visit_datetime desc) as rnum 
      from customers c
     ) c
where rnum=1 
group by cust_id