这里有另一个新手问题,
我有以下选择:
SELECT
DATEADD(dd, 0, DATEDIFF(dd, 0, CIC.StatusDateTime)) AS ST_Date,
CIC.UserId,
MIN(CIC.StatusDateTime) as Beginning,
MAX(CIC.EndDateTime) as ending,
DATEPART(hour,MAX(CIC.EndDateTime) - MIN(CIC.StatusDateTime)) * 3600
+ DATEPART(minute, MAX(CIC.EndDateTime) - MIN(CIC.StatusDateTime)) * 60
+ DATEPART(second,MAX(CIC.EndDateTime) - MIN(CIC.StatusDateTime)) AS Login_Time,
SUM(CASE WHEN RS.AGT_STATE_GRP = 5 THEN StateDuration ELSE 0 END) AS Lunch,
SUM(CASE WHEN RS.AGT_STATE_GRP = 3 THEN StateDuration ELSE 0 END) AS Break_,
SUM(CASE WHEN RS.AGT_STATE_GRP = 4 THEN StateDuration ELSE 0 END) AS Coaching,
SUM(CASE WHEN RS.AGT_STATE_GRP = 10 THEN StateDuration ELSE 0 END) AS SYS_Down,
SUM(CASE WHEN RS.AGT_STATE_GRP = 7 THEN StateDuration ELSE 0 END) AS Training,
SUM(CASE WHEN RS.AGT_STATE_GRP = 6 THEN StateDuration ELSE 0 END) AS Meeting,
SUM(CASE WHEN RS.AGT_STATE_GRP = 11 THEN StateDuration ELSE 0 END) AS Default_
FROM dbo.CIC_AgentActivityLog_TMP AS CIC
INNER JOIN dbo.REF_AGT_STATES AS RS
ON CIC.StatusKey = RS.AGT_STATE_DESC
Where CIC.StatusDateTime >='2011-09-01'
GROUP BY CIC.StatusDateTime, CIC.UserId, CIC.EndDateTime
ORDER BY ST_Date,UserID,StatusDateTime;
使用它,我的结果似乎根本不是分组。我想要的是一行包含date,userid,firstlogin,lastlogin,totallogintime,然后是一堆代码,其持续时间以秒为单位。我有一些非常相似的另一个数据源工作正常。
我尝试转换并使用第一个日期的演员,认为最后的00:00:00可能会有冲突,但无济于事。我不确定你们可能需要的更多信息,请告诉我。
* Edit1 - 我的MIN(CIC.StatusDateTime)作为Beginning,MAX(CIC.EndDateTime)作为结尾似乎没有帮助。对于代理,我有三行,它们都有不同的开始和结束时间。最小值和最大值并没有给我一整天的实际最小值和最大值。
这是源数据:
userID StatusDateTime StatusKey EndDateTime StateDuration
bBarsby 9/1/2011 11:36 Out of the office 9/1/2011 11:36 0
bBarsby 9/1/2011 11:36 Out of the office 9/1/2011 11:36 5
bBarsby 9/1/2011 11:36 Available 9/1/2011 14:18 9711
bBarsby 9/1/2011 14:18 Away from desk 9/1/2011 14:39 1261
bBarsby 9/1/2011 14:39 Available 9/1/2011 19:51 18693
bBarsby 9/1/2011 19:51 Out of the office 9/1/2011 19:51 4
bBarsby 9/1/2011 19:51 Out of the office 9/1/2011 19:51 0
以下是结果
ST_Date UserID Beginning Ending LoginTime Lunch Break Coaching Etc.
9/1/2011 0:00 bBarsby 9/1/2011 11:36 9/1/2011 14:18 9711 0 0 0 0
9/1/2011 0:00 bBarsby 9/1/2011 14:18 9/1/2011 14:39 1261 0 0 0 0
9/1/2011 0:00 bBarsby 9/1/2011 14:39 9/1/2011 19:51 18693 0 0 0 0
答案 0 :(得分:4)
您按CIC.StatusDateTime
和CIC.EndDateTime
进行分组...只需:
GROUP BY DATEADD(dd, 0, DATEDIFF(dd, 0, CIC.StatusDateTime)), CIC.UserId
答案 1 :(得分:1)
Aaron Bertrand有正确的想法。你绝对不应该在结束日期定义组。
以下是用该组重写的查询。
SELECT
sub.ST_Date,
sub.UserId,
MIN(sub.StatusDateTime) as Beginning,
MAX(sub.EndDateTime) as ending,
DATEPART(hour,MAX(sub.EndDateTime) - MIN(sub.StatusDateTime)) * 3600
+ DATEPART(minute, MAX(sub.EndDateTime) - MIN(sub.StatusDateTime)) * 60
+ DATEPART(second,MAX(sub.EndDateTime) - MIN(sub.StatusDateTime)) AS Login_Time,
SUM(CASE WHEN sub.AGT_STATE_GRP = 5 THEN StateDuration ELSE 0 END) AS Lunch,
SUM(CASE WHEN sub.AGT_STATE_GRP = 3 THEN StateDuration ELSE 0 END) AS Break_,
SUM(CASE WHEN sub.AGT_STATE_GRP = 4 THEN StateDuration ELSE 0 END) AS Coaching,
SUM(CASE WHEN sub.AGT_STATE_GRP = 10 THEN StateDuration ELSE 0 END) AS SYS_Down,
SUM(CASE WHEN sub.AGT_STATE_GRP = 7 THEN StateDuration ELSE 0 END) AS Training,
SUM(CASE WHEN sub.AGT_STATE_GRP = 6 THEN StateDuration ELSE 0 END) AS Meeting,
SUM(CASE WHEN sub.AGT_STATE_GRP = 11 THEN StateDuration ELSE 0 END) AS Default_
FROM
(
SELECT
DATEADD(dd, 0, DATEDIFF(dd, 0, CIC.StatusDateTime)) AS ST_Date
CIC.UserId,
CIC.StatusDateTime,
CIC.EndDateTime,
RS.AGT_STATE_GRP,
StateDuration
FROM
dbo.CIC_AgentActivityLog_TMP AS CIC
JOIN dbo.REF_AGT_STATES AS RS
ON CIC.StatusKey = RS.AGT_STATE_DESC
WHERE CIC.StatusDateTime >='2011-09-01'
) sub
GROUP BY sub.ST_Date, sub.UserId
ORDER BY sub.ST_Date, sub.UserId
答案 2 :(得分:0)
在组中使用ST_Date,因为这是表格中的列名。
GROUP BY CIC.StatusDateTime, CIC.UserId, CIC.EndDateTime
to
GROUP BY CIC.ST_DATE, CIC.UserId, CIC.EndDateTime
编辑:
group by DATEADD(dd, 0, DATEDIFF(dd, 0, CIC.StatusDateTime)),CIC.UserId, MAX(CIC.EndDateTime)