在sql中选择具有最新日期的行,每个ID重复多次 [英] select rows in sql with latest date for each ID repeated multiple times

查看:86
本文介绍了在sql中选择具有最新日期的行,每个ID重复多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,每个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

这篇关于在sql中选择具有最新日期的行,每个ID重复多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆