Get the smallest datetime value for each day in SQL database

只谈情不闲聊 提交于 2019-12-13 19:25:11

问题


I have a database that are storing datetime values every minute and I wanna get the smallest value for each day.

My query looks like this:

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')

And my values is disposed like this:

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:


You can group by id and trunc date value:

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)


来源:https://stackoverflow.com/questions/36063358/get-the-smallest-datetime-value-for-each-day-in-sql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!