SQL Server Select 1 row per hour

6mzjoqzu  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(120)

I am using SQL Server 2019. I have a table in my database which contains a column called update_date. For example, if I have the following data, I would like to import one data per hour within the last 24 hours.

2023-11-28 14:50:55
2023-11-28 15:10:33
2023-11-28 15:11:33
2023-11-28 15:53:33
2023-11-28 16:13:35
2023-11-28 16:56:49
2023-11-28 16:56:50
2023-11-29 00:37:02
2023-11-29 00:40:02
2023-11-29 00:41:02
2023-11-29 00:42:02
2023-11-29 00:44:02

I would like to return

2023-11-28 14:50:55
2023-11-28 15:10:33
2023-11-28 16:13:35
2023-11-29 00:37:02
SELECT *
FROM table
WHERE update_date >= DATEADD(HOUR , -24, GETDATE())

I don't know how to add a query. Please help me

gcmastyq

gcmastyq1#

You can do it using GROUP BY and MIN() :

SELECT MIN(update_date) as mints
FROM mytable
WHERE update_date >= DATEADD(HOUR , -24, GETDATE())
GROUP BY CAST(update_date as DATE), DATEPART(HOUR, update_date);

相关问题