如何找到两小时差异的平均时间?
这是我的变体,但输出不能仅按名称分组。
SELECT NAME, DATEDIFF(HOUR, START_DATE, END_DATE)
FROM [dbo].[DB1]
GROUP BY NAME, START_DATE, END_DATE
示例:
1 DAY
START_DATE 2017-05-02 05-00-00-000
END_DATE 2017-05-03 00-00-00-000
RESULT: 7 HOURS
2 DAY
START_DATE 2017-05-02 07-00-00-000
END_DATE 2017-05-03 12-00-00-000
RESULT: 5 HOURS
AND AVG RESULT = 6
答案 0 :(得分:0)
在子查询中生成DATEDIFF
,然后平均:
SELECT
[Name],
AVG( [Time] ) AS AverageTime
FROM
(
SELECT
NAME,
DATEDIFF( hour, START_DATE, END_DATE ) AS [Time]
FROM
dbo.DB1
GROUP BY
NAME,
START_DATE,
END_DATE
) AS subquery
答案 1 :(得分:0)
你的意思是,
SELECT NAME, AVG(DATEDIFF(HOUR, START_DATE, END_DATE))
FROM [dbo].[DB1]
GROUP BY NAME