也許還有用於存檔此另一個辦法,但我是如下: -
演示: -
DDL & DML
Create table Test (
[Date] date ,
[time] time(7),
col1 char (1),
col2 char (1) ,
col3 char(1)
)
go
insert into Test values ('02/02/2017' , '1:15' , 'a' , 'b' , 'c')
insert into Test values ('02/02/2017' , '1:35' , 'w' , 'e' , 'r')
insert into Test values ('02/02/2017' , '2:16' , 'q' , 'w' , 'e')
insert into Test values ('02/02/2017' , '3:48' , 'z' , 'x' , 'c')
insert into Test values ('02/02/2017' , '3:58' , 'b' , 'n' , 'm')
go
select * from Test
結果: -
查詢
select [Date] ,
dateadd(minute,abs(datepart(minute,[time]) - 60),[time]) as [time],
col1 , col2 , col3
from test
where
datepart(hour,[time])
in
(
select
datepart(hour,[time])
from Test
group by datepart(hour,[time])
)
AND
abs(datepart(minute,[time]) - 60)
in
(
select
min(abs(datepart(minute,[time]) - 60))
from Test
group by datepart(hour,[time])
)
結果: -
什麼是時間列的數據類型,以及您使用的是哪個版本的sql server? –
@ZoharPeled數據類型是時間(7),我正在使用SQL Server 2014 Management Studio(12。0) – user7409370