我有一个SQL选择,用英语编写非常简单但我无法实现在SQL中翻译它。这是:
我希望用户获得最新活动的ID。
表格如下:
| COLUMN_NAME | DATA_TYPE |
|-------------|-----------|
| ID | NUMBER |
| OCCURED | DATE |
| USER_ID | NUMBER |
我尝试使用Max()
,一些GroupBys:
SELECT l.user_id, MAX(l.OCCURED)
FROM USER_STATS.LOG l
group by l.user_id, l.occured
order by l.OCCURED DESC
但我没有成功。
我怎么能这样做?
答案 0 :(得分:1)
SELECT l.user_id, MAX(l.OCCURED) as OCCURED
FROM USER_STATS.LOG l
group by l.user_id
order by OCCURED DESC
仅限GROUP BY
user_id。
答案 1 :(得分:1)
我重新打开了这个问题,因为我认为这是最简单的方法:
使用单个聚合的一种方法是使用first
:
SELECT l.user_id, MAX(l.OCCURED),
MAX(ID) KEEP (DENSE_RANK FIRST ORDER BY l.OCCURRED DESC) as id_at_max
FROM USER_STATS.LOG l
GROUP BY l.user_id;