我有如下表格,其中没有主键。
COL_Date Col_Text Col_Lang
23-July-17 Hello de
23-July-17 Hello en
19-July-17 Hello de
18-July-17 Hello en
17-July-17 Hello es
我想获取每种语言的最新行。我可以使用以下查询获取2列,但它不包括Col_Text列,有没有办法做到这一点?添加Col_Text以选择错误的结果。
SELECT DISTINCT Col_Lang, MAX(COL_Date)
FROM table1
GROUP BY Col_Lang
ORDER BY MAX(COL_Date) DESC, Col_Lang
答案 0 :(得分:3)
您可以使用row_number()
:
select COL_Date, Col_Text, Col_Lang
from (select t1.*,
row_number() over (partition by col_lang order by col_date desc) as seqnum
from t1
) t1
where seqnum = 1;
返回整行。在Oracle中,您还可以使用keep
:
SELECT Col_Lang, MAX(COL_Date),
MAX(col_text) KEEP (DENSE_RANK FIRST ORDER BY Col_date) as col_text
FROM table1
GROUP BY Col_Lang
ORDER BY MAX(COL_Date) DESC, Col_Lang;