为每个重复多次选择具有最新日期的sql中的行

2021-12-28 00:00:00 sql hive mysql

我有一个表格,其中每个 ID 重复 3 次.每行的每个 id 前面都有一个日期.
我想为日期最新的每个 ID 选择整行.此表中共有 370 列,我希望在选择该行时选择所有列.

I have a table where each ID is repeated 3 times. there is a date in front of each id in each row.
I want to select entire row for each ID where date is latest. There are total 370 columns in this table i want all columns to get selected when i select that row.

样品 -

ID   Name    Date        Marks    ..    ..  ..   
1     XY     4/3/2017     27
1     fv     4/3/2014     98
1     jk     4/3/2016     09
2     RF     4/12/2015    87
2     kk     4/3/2009     56
2     PP     4/3/2011     76
3     ee     4/3/2001     12
3     ppp    4/3/2003     09
3     lll    4/3/2011     23

答案应该是

ID   Name    Date        Marks    ..    ..  ..   
1     XY     4/3/2017     27      
2     RF     4/12/2015    87
3     lll    4/3/2011     23  

我正在尝试如下 -

select distinct ID,*,max(date) as maxdate from table

我也在 Hive 中尝试这个.所以不确定某些 sql 函数是否在 Hive 中不起作用

Also i am trying this in Hive . so not sure if some sql functions dont work in Hive

谢谢

推荐答案

这个问题以前有人问过.请参阅这个 问题.

This question has been asked before. Please see this question.

使用已接受的答案并将其调整到您遇到的问题:

Using the accepted answer and adapting it to your problem you get:

SELECT tt.*
FROM myTable tt
INNER JOIN
    (SELECT ID, MAX(Date) AS MaxDateTime
    FROM myTable
    GROUP BY ID) groupedtt 
ON tt.ID = groupedtt.ID 
AND tt.Date = groupedtt.MaxDateTime

相关文章