我们目前有以下专栏:
CustomerID int
ArrivalDateTime datetime
CheckPoint varchar(20)
DepartureDateTime datetime
CheckPoint列根据特定查询记录24小时格式时间。
CustomerID ArrivalDateTime CheckPoint DepartureDateTime
1 2015-05-03 08:15 0800 2015-05-03 08:30
2 2015-05-04 13:15 1300 2015-05-04 15:30
我需要的是用CheckPoint时间替换ArrivalDateTime的时间。所以它如下所示:
CustomerID ArrivalDateTime DepartureDateTime
1 2015-05-03 08:00 2015-05-03 08:30
2 2015-05-04 13:00 2015-05-04 15:30
这样我就能从ArrivalDateTime到DepartureDateTime获得时差。 预期成果:
CustomerID ArrivalDateTime DepartureDateTime TimeInMinutes
1 2015-05-03 08:00 2015-05-03 08:30 30
2 2015-05-04 13:00 2015-05-04 15:30 150
答案 0 :(得分:2)
试试这个:
select
CustomerID
, DATEADD(day, DATEDIFF(day, 0, ArrivalDateTime), STUFF(CheckPoint_,3,0,':')) ArrivalDateTime
, DepartureDateTime
, DATEDIFF(mi, DATEADD(day, DATEDIFF(day, 0, ArrivalDateTime), STUFF([CheckPoint],3,0,':')), DepartureDateTime) as TimeInMinutes
from t;
如果您不想重复这些功能,可以将它们移动到cte或派生表中,如下所示:
select
CustomerID
, ArrivalDateTime
, DepartureDateTime
, DATEDIFF(mi, ArrivalDateTime, DepartureDateTime) as TimeInMinutes
from (
select
CustomerID
, DATEADD(day, DATEDIFF(day, 0, ArrivalDateTime), STUFF([CheckPoint],3,0,':')) ArrivalDateTime
, DepartureDateTime
from t) a
;
答案 1 :(得分:1)
试试这个
;WITH cte
AS (SELECT customerid,
ArrivalDateTime = Cast(CONVERT(VARCHAR(20), Cast(ArrivalDateTime AS DATE))
+ ' ' + Stuff(CheckPoint_, 3, 0, ':') AS DATETIME),
departuredatetime
FROM yourtable)
SELECT customerid,
arrivaldatetime,
departuredatetime,
TimeInMinutes = Datediff(minute, arrivaldatetime, departuredatetime)
FROM CTE
答案 2 :(得分:1)
select
CA.RevisedArrivalDateTime
, DepartureDateTime
, datediff(minute,CA.RevisedArrivalDateTime,DepartureDateTime)
from yourtable
cross apply (
select dateadd(minute
, convert(int,left([CheckPoint],2)) * 60
+ convert(int,right([CheckPoint],2))
,dateadd(day, datediff(day,0, ArrivalDateTime ), 0))
) CA (RevisedArrivalDateTime)
通过将CheckPoint转换为几分钟,可以避免使用varchar转换为日期时间,从而提高效率。
此外,通过使用交叉应用,可以通过别名重用计算的新日期时间。
答案 3 :(得分:0)
尝试以下方法:
SELECT CustomerID
,CAST(CAST(ArrivalDateTime AS DATE) AS DATETIME) + CAST(CONCAT(LEFT([CheckPoint], 2), ':', RIGHT([CheckPoint], 2)) AS DATETIME) AS ArrivalDateTime
,DepartureDateTime
,DATEDIFF(MINUTE, CAST(CAST(ArrivalDateTime AS DATE) AS DATETIME) + CAST(CONCAT(LEFT([CheckPoint], 2), ':', RIGHT([CheckPoint], 2)) AS DATETIME), DepartureDateTime) AS TimeInMinutes
FROM @tbl
带输出的示例数据
DECLARE @tbl TABLE (CustomerID INT, ArrivalDateTime DATETIME, [CheckPoint] Varchar(20), DepartureDateTime DATETIME)
INSERT @tbl
SELECT 1 ,'2015-05-03 08:15', '0800' , '2015-05-03 08:30' UNION ALL
SELECT 2 ,'2015-05-04 13:15', '1300' , '2015-05-04 15:30'
SELECT CustomerID
,CAST(CAST(ArrivalDateTime AS DATE) AS DATETIME) + CAST(CONCAT(LEFT([CheckPoint], 2), ':', RIGHT([CheckPoint], 2)) AS DATETIME) AS ArrivalDateTime
,DepartureDateTime
,DATEDIFF(MINUTE, CAST(CAST(ArrivalDateTime AS DATE) AS DATETIME) + CAST(CONCAT(LEFT([CheckPoint], 2), ':', RIGHT([CheckPoint], 2)) AS DATETIME), DepartureDateTime) AS TimeInMinutes
FROM @tbl
CustomerID ArrivalDateTime DepartureDateTime TimeInMinutes
1 2015-05-03 08:00:00.000 2015-05-03 08:30:00.000 30
2 2015-05-04 13:00:00.000 2015-05-04 15:30:00.000 150