请参阅下表了解更多详情。
1 vishal 10 7/4/2017 1:00:00 PM
2 rahul 12 7/4/2017 2:00:00 PM
3 sudha 15 7/4/2017 3:00:00 PM
4 vishal 10 7/4/2017 4:00:00 PM
5 vishal 10 7/4/2017 2:30:00 PM
6 vishal 10 7/4/2017 12:30:00 PM
7 vishal 10 6/4/2017 10:30:00 PM
8 vishal 10 10/4/2017 12:00:00 AM
我想要低于输出
vishal 10 7/4/2017 12:30:00 PM 4:00:00 PM 3.5hrs
注意:必须在SQL Server 2003上运行查询。
答案 0 :(得分:0)
如果我理解你的问题,这可以做到这一点
create table #table1 (field1 int, field2 varchar(10), field3 int, field4 datetime)
insert into #table1 values
(1, 'vishal', 10, '20170704 1:00:00'),
(2, 'rahul', 12, '20170704 2:00:00'),
(3, 'sudha', 15, '20170704 3:00:00'),
(4, 'vishal', 10, '20170704 4:00:00'),
(5, 'vishal', 10, '20170704 2:30:00'),
(6, 'vishal', 10, '20170704 00:30:00'),
(7, 'vishal', 10, '20170604 10:30:00'),
(8, 'vishal', 10, '20171004 00:00:00')
select field2,
field3,
convert(date, field4),
min(field4),
max(field4),
Datediff(second,Min(field4),Max(field4))/3600.00
from #table1
group by field2, field3, convert(date, field4)
drop table #table1
结果如下:
rahul 12 2017-07-04 4/07/2017 2:00:00 4/07/2017 2:00:00 0
sudha 15 2017-07-04 4/07/2017 3:00:00 4/07/2017 3:00:00 0
vishal 10 2017-06-04 4/06/2017 10:30:00 4/06/2017 10:30:00 0
vishal 10 2017-07-04 4/07/2017 0:30:00 4/07/2017 4:00:00 3.5
vishal 10 2017-10-04 4/10/2017 4/10/2017 0
答案 1 :(得分:0)
希望这会奏效,
SELECT Name,
Max(Value) AS Value,
Cast(Date_Time as Date) AS Date,
Max(Date_Time) AS Max_Date_Time,
Min(Date_Time) AS Min_Date_Time,
Datediff(second,Min(Date_Time),Max(Date_Time))/3600.00 AS Time_Diff
FROM #Your_Table
Group by Name, Cast(Date_Time as Date)
注意:在SQL Server 2008中工作
答案 2 :(得分:0)
使用DATEDIFF,LTRIM,RIGHT和Group By
select name as Name,Number as Value,
convert(date,startdate) as [Date],
LTRIM(RIGHT(CONVERT(VARCHAR(20),MIN(convert(datetime,convert(varchar,startdate,109))),22),11)) AS MIN_Date,
LTRIM(RIGHT(CONVERT(VARCHAR(20),MAX(convert(datetime,convert(varchar,startdate,109))),22),11)) AS MAX_Date,
convert(varchar(50),convert(numeric(8,1),(DATEDIFF(second,MIN(convert(datetime,convert(varchar,startdate,109))),max(startdate))/3600.00)))+' hrs' as TimeDifference
from my_table_new group by name,convert(date,startdate),Number
输出:
Name Value Date MIN_Date MAX_Date TimeDifference
rahul 12 2017-07-04 2:00:00 PM 2:00:00 PM 0.0 hrs
sudha 15 2017-07-04 3:00:00 PM 3:00:00 PM 0.0 hrs
vishal 10 2017-06-04 10:30:00 PM 10:30:00 PM 0.0 hrs
vishal 10 2017-07-04 12:30:00 PM 4:00:00 PM 3.5 hrs
vishal 10 2017-10-04 12:00:00 AM 12:00:00 AM 0.0 hrs