帮助完成不同的行和数据排序(help with distinct rows and data ordering)

编程入门 行业动态 更新时间:2024-10-10 00:21:46
帮助完成不同的行和数据排序(help with distinct rows and data ordering)

如果我有记录:

Row Date, LocationID, Account 1 Jan 1, 2008 1 1000 2 Jan 2, 2008 1 1000 3 Jan 3, 2008 2 1001 4 Jan 3, 2008 1 1001 5 Jan 3, 2008 3 1001 6 Jan 4, 2008 3 1002

我需要获取行( date , locatinid , account )行中每个不同的locationid最近的日期:

4 Jan 3, 2008 1 1001 3 Jan 3, 2008 2 1001 6 Jan 4, 2008 3 1002

If I have records:

Row Date, LocationID, Account 1 Jan 1, 2008 1 1000 2 Jan 2, 2008 1 1000 3 Jan 3, 2008 2 1001 4 Jan 3, 2008 1 1001 5 Jan 3, 2008 3 1001 6 Jan 4, 2008 3 1002

I need to get the row (date, locatinid, account) where the row has the most recent date for each distinct locationid:

4 Jan 3, 2008 1 1001 3 Jan 3, 2008 2 1001 6 Jan 4, 2008 3 1002

最满意答案

我认为这将工作:

SELECT t1.* FROM table t1 JOIN (SELECT MAX(Date), LocationID FROM table GROUP BY Date, LocationID) t2 on t1.Date = t2.Date and t1.LocationID = t2.LocationID

I think this would work:

SELECT t1.* FROM table t1 JOIN (SELECT MAX(Date), LocationID FROM table GROUP BY Date, LocationID) t2 on t1.Date = t2.Date and t1.LocationID = t2.LocationID

更多推荐

本文发布于:2023-07-06 07:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   distinct   rows   data   ordering

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!