如何在SQL Server中减去两个数据项
在我的数据库中,有一个表包含(ID、名称、时间、类型) 我需要构造一个查询来输出时差(OUT-IN) 例如:如何在SQL Server中减去两个数据项,sql,sql-server,database,Sql,Sql Server,Database,在我的数据库中,有一个表包含(ID、名称、时间、类型) 我需要构造一个查询来输出时差(OUT-IN) 例如: Osama, 5 Osama, 5 如果你认为你的时间是连续的,你可以做Max(TimeRecorded)。这假设您的ID是连续的。您可以使用检查约束控制其中任何一个 declare @test table ( Id int, Name varchar(50), TimeRecorded time, TypeOfTimeRecording varchar(3) ) inser
Osama, 5
Osama, 5
如果你认为你的时间是连续的,你可以做Max(TimeRecorded)。这假设您的ID是连续的。您可以使用检查约束控制其中任何一个
declare @test table (
Id int, Name varchar(50), TimeRecorded time, TypeOfTimeRecording varchar(3)
)
insert into @test values (1, 'Osama', CONVERT(time, '12:15'), 'IN')
insert into @test values (2, 'Osama', CONVERT(time, '12:20'), 'OUT')
insert into @test values (3, 'Osama', CONVERT(time, '12:25'), 'IN')
insert into @test values (4, 'Osama', CONVERT(time, '12:30'), 'OUT')
select testOut.Name
,testOut.TimeRecorded
,testIn.TimeRecorded
,DATEDIFF(minute, testIn.TimeRecorded, testOut.TimeRecorded) as [Out - In]
from @test testOut
inner join
@test testIn on testIn.Id = (select MAX(Id) from @test where Name = testOut.Name and Id < testOut.Id and TypeOfTimeRecording = 'IN')
where testOut.TypeOfTimeRecording = 'OUT'
declare@testtable(
Id int,Name varchar(50),TimeRecorded time,TypeOfTimeRecording varchar(3)
)
插入@test values(1,'Osama',CONVERT(time,'12:15'),'IN')
插入@test values(2,'Osama',CONVERT(time,'12:20'),'OUT')
插入@test values(3,'Osama',CONVERT(time,'12:25'),'IN')
插入@test values(4,'Osama',CONVERT(time,'12:30'),'OUT')
选择testOut.Name
,testOut.TimeRecorded
时间记录
,DATEDIFF(分钟,testIn.TimeRecorded,testOut.TimeRecorded)为[Out-In]
来自@test testOut
内连接
@test testIn on testIn.Id=(从@test中选择MAX(Id),其中Name=testOut.Name和Id
此处的TestData
CTE纯粹用于测试目的。另外,我注意到您的数据有一个错误。上次我检查的14:15 AM
不是有效时间。它是14:15
(通过24小时时钟)或2:15 AM
(通过12小时时钟)。此外,此解决方案需要SQL Server 2005或更高版本
With TestData As
(
Select 1 As Id, 'Osama' As Name, '12:15' As Time, 'IN' As Type
Union All Select 2, 'Osama', '12:20', 'OUT'
Union All Select 3, 'Osama', '14:15', 'IN'
Union All Select 4, 'Osama', '14:20', 'OUT'
)
, CheckInCheckOut As
(
Select Id, Name, Time, Type
, Row_Number() Over ( Partition By Name, Type Order By Time ) As Num
From TestData
)
Select C1.Name
, DateDiff( mi, CAST(C1.Time as datetime), Cast(C2.Time As datetime) ) As [OUT-IN]
From CheckInCheckOut As C1
Join CheckInCheckOut As C2
On C2.Name = C1.Name
And C2.Type = 'OUT'
And C2.Num = C1.Num
Where C1.Type = 'IN'
declare @test table (
Id int, Name varchar(50), TimeRecorded time, TypeOfTimeRecording varchar(3)
)
insert into @test values (1, 'Osama', CONVERT(time, '12:15'), 'IN')
insert into @test values (2, 'Osama', CONVERT(time, '12:20'), 'OUT')
insert into @test values (3, 'Osama', CONVERT(time, '12:25'), 'IN')
insert into @test values (4, 'Osama', CONVERT(time, '12:30'), 'OUT')
select testOut.Name
,testOut.TimeRecorded
,testIn.TimeRecorded
,DATEDIFF(minute, testIn.TimeRecorded, testOut.TimeRecorded) as [Out - In]
from @test testOut
inner join
@test testIn on testIn.Id = (select MAX(Id) from @test where Name = testOut.Name and Id < testOut.Id and TypeOfTimeRecording = 'IN')
where testOut.TypeOfTimeRecording = 'OUT'
With TestData As
(
Select 1 As Id, 'Osama' As Name, '12:15' As Time, 'IN' As Type
Union All Select 2, 'Osama', '12:20', 'OUT'
Union All Select 3, 'Osama', '14:15', 'IN'
Union All Select 4, 'Osama', '14:20', 'OUT'
)
, CheckInCheckOut As
(
Select Id, Name, Time, Type
, Row_Number() Over ( Partition By Name, Type Order By Time ) As Num
From TestData
)
Select C1.Name
, DateDiff( mi, CAST(C1.Time as datetime), Cast(C2.Time As datetime) ) As [OUT-IN]
From CheckInCheckOut As C1
Join CheckInCheckOut As C2
On C2.Name = C1.Name
And C2.Type = 'OUT'
And C2.Num = C1.Num
Where C1.Type = 'IN'