组中的DATEDIFF导致错误

时间:2017-05-18 11:54:09

标签: sql sql-server tsql

我发布了一个新问题,因为之前的问题已经发生了一些变化。我正在使用此查询:

SELECT DATEDIFF(SECOND,servicestart,CheckinTime) abondendedTime FROM Location L INNER JOIN
     Interaction I
     ON L.Id = I.IdLocation
WHERE ServiceEnd = '00:00:00.000'
GROUP BY L.Id, L.Description,DATEDIFF(SECOND,servicestart,CheckinTime)

它返回700行,因为我不需要这部分

GROUP BY中的DATEDIFF(第二,servicestart,CheckinTime)但是如果我没有把它放在GROUP BY中,我会收到错误:

Column 'Interaction.ServiceStart' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

我想要这个结果集

http://prntscr.com/f993xe

我需要做这些计算。

http://prntscr.com/f995dk

我已经完成了这部分

SELECT L.Id,
       L.Description AS 'Location',
       COUNT(L.Id) Interactions,
       SUM(CASE WHEN I.ServiceEnd = '00:00:00.000' THEN 1 ELSE 0 END) Abandoned,
       (SUM(CASE WHEN I.ServiceEnd = '00:00:00.000' THEN 1 ELSE 0 END) *100)/COUNT(I.Id) AbandonedPercent
       FROM Location L
       INNER JOIN Interaction I
       ON L.Id = I.IdLocation
       GROUP BY L.Id,L.Description

需要计算

Average Abandon Time,Members,Repeat Members,Repeat Visits,Repeat/Interaction,Average Wait,Average Service

以上第一个查询是针对平均放弃时间

1 个答案:

答案 0 :(得分:0)

我认为这就是你想要的:

SELECT L.Id, L.Description,
       SUM(DATEDIFF(SECOND, servicestart, CheckinTime)) as abondendedTime
FROM Location L INNER JOIN
     Interaction I
     ON L.Id = I.IdLocation
WHERE ServiceEnd = '00:00:00.000'
GROUP BY L.Id, L.Description;

与上一个问题不同,WHERE中的列与用于DATEDIFF()的列不同。