我有一张这样的表,
Date User
15-06-2018 A
16-06-2018 A
15-06-2018 B
14-06-2018 C
16-06-2018 C
我想得到像这样的输出,
Date User
16-06-2018 A
15-06-2018 B
16-06-2018 C
我尝试了Select Max(date),User from Table group by User
答案 0 :(得分:2)
根据您的评论,我假设当您group by
时,这80个列中有重复的结果。假设是这样的,这里有一个选项,row_number
总是为每个用户返回1行:
select *
from (
select *, row_number() over (partition by user order by date desc) rn
from yourtable
) t
where rn = 1
答案 1 :(得分:1)
您可以使用 correlation subquery
:
select t.*
from table t
where date = (select max(t1.date)
from table t1
where t1.user = t.user
);
但是,我还建议row_number()
:
select top (1) with ties *
from table t
order by row_number() over (partition by user order by date desc);
答案 2 :(得分:0)
您还可以使用排名功能
SELECT User, Date
FROM
(
SELECT User, Date
, Row_id = Row_Number() OVER (Partition by User, ORDER BY User, Date desc)
FROM table
)q
WHERE Row_Id = 1
答案 3 :(得分:0)
我建议你这个
dplyr::filter