Name Longititue latutute Time
tharindu 79.94148 6.9748404 00:15:47
shane 79.8630765 6.8910388 13:23:24
shane 79.862815 6.8909349 14:41:29
shane 79.8628665 6.8911084 09:39:33
shane 79.8626956 6.890992 11:00:07
shane 79.8628831 6.89099 11:43:00
我要获得如下结果集
shane 79.862815 6.8909349 14:41:29
tharindu 79.94148 6.9748404 00:15:47
我该如何编写sql查询以获取结果集,并从最大时间获取结果集
答案 0 :(得分:0)
您可以尝试使用ROW_NUMBER
窗口功能。
SELECT Name,Longititue,latutute,[Time]
FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY Name ORDER BY [Time] DESC) rn
FROM T
)t1
WHERE rn = 1
答案 1 :(得分:0)
您也可以尝试使用相关子查询
select * from tablename a
where Time in (select max(Time) from tablename b where a.name=b.name)
答案 2 :(得分:0)
您可以使用相关的子查询
select t.* from table_name t
where t.[Time]=( select max([Time]) from table_name t1 where t1.Name=t.Name)
答案 3 :(得分:0)
这是一种无需正式子查询即可使用ROW_NUMBER
的方法:
SELECT TOP 1 WITH TIES
Name,
Longititue,
latutute,
Time
FROM yourTable
ORDER BY
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY [Time] DESC);
答案 4 :(得分:0)
您可以使用CTE(通用表表达式)和排名函数来实现此目的。
SQL查询:
WITH CTE AS
(
SELECT Name,Longititue,latutute,Time,DENSE_RANK() OVER(PARTITION BY Name ORDER BY time desc) as RN
FROM MaxTime
)
SELECT * FROM CTE
WHERE RN = 1