本文介绍了获取SQL数据库中每一天的最小日期时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的查询如下所示:

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

推荐答案

您可以按 id 和截断日期值分组:

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)

这篇关于获取SQL数据库中每一天的最小日期时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 16:56