答案 0 :(得分:1)
您可以使用窗口函数返回所有行
select
*,
min([time]) over (partition by [date] order by [time])
from YourTable
或者只是将其删除的汇总
select *
from YourTable
inner join
(select whatever, min(FullDate) dt
from yourtable
group by whatever) x on x.whatever = YourTable.whatever and x.dt = YourTable.FullDate
如果无论什么列都不重要,而您只需要日期和时间:
Select
[date],
min([time])
from YourTable
group by [Date]
答案 1 :(得分:1)
要删除,您可以使用具有ROW_NUMBER窗口功能的CTE
;WITH cteDups
AS(
SELECT *, RN=ROW_NUMBER()OVER (PARTITION BY M.Date ORDER BY M.Time ASC)
FROM dbo.yourtable M
)
--SELECT *
DELETE
FROM cteDups D WHERE D.RN > 1
答案 2 :(得分:1)
保留某些记录并删除其余记录的最简单方法是使用带有窗口功能的CTE进行排名(或添加行号)。检查一下:
;WITH EarliestHourEveryDay AS (
SELECT
whatever
,FullDate
,[date]
,[time]
,rn = ROW_NUMBER() OVER (PARTITION BY [date] ORDER BY [time])
FROM TableName
)
SELECT *
FROM EarliestHourEveryDay
WHERE rn = 1
/*
DELETE FROM EarliestHourEveryDay
WHERE rn > 1
*/
我已注释掉delete语句,以便您可以首先对其进行测试。按原样运行CTE,如果结果集包含所需的确切行,请从CTE中删除SELECT语句,并取消注释DELETE语句,这样就可以了。
答案 3 :(得分:0)
按天分组,然后选择最短时间。
答案 4 :(得分:0)
使用MIN函数和GROUP BY子句。
类似的东西:
SELECT date, MIN(time) AS EarliestTime
FROM MyTable
GROUP BY date
ORDER BY date ASC
以下是此工作的示例:SQL Fiddle