获取SQL数据库中每天的最小日期时间值

时间:2016-03-17 14:23:56

标签: sql-server sql-server-2012

我有一个每分钟存储日期时间值的数据库,我想获得每天最小的值。

我的查询如下:

SELECT  lVehicleId, dtVehicleHistoryDate 
FROM  VehicleHistory
WHERE  lVehicleId = 1146 AND dtVehicleHistoryDate > '2016-01-01 00:00:00' AND dtVehicleHistoryDate < '2016-01-31 00:00:00' AND ((dtTimeReceived - dtVehicleHistoryDate) > '00:30:00')

我的价值观是这样处理的:

Id      History date

1146    2015-01-26 23:03:50.000

1146    2015-01-26 23:04:50.000

1146    2015-01-26 23:05:50.000


1146    2015-02-19 01:33:42.000

1146    2015-02-19 01:34:42.000

1146    2015-02-19 01:35:42.000

1146    2015-02-19 01:36:42.000

1 个答案:

答案 0 :(得分:0)

您可以按ID和截断日期值进行分组:

SELECT t.id,cast(t.history_date as DATE),min(t.history_date)
FROM YourTable t
GROUP BY t.id,cast(t.history_date as DATE)