在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