0

当我运行它时(SQL Server 2008 R2):

SELECT [UserId]
      ,[StateDuration]
      ,[StatusKey]  
FROM [AgentActivityLog]
WHERE [StatusDateTime] between '2012-01-01' and '2012-12-31'

我得到每个 UserId 的一行,如下所示: UserId StatusDateTime StatusKey

cfrinco      3456   Meeting
echartl      23456  Available
mbarbewar    3256   Lunch
bsantiago    46565  Available
bsantiago    79     Follow Up
fburger      5464   Available

它告诉我每个用户处于每个状态的时间(以秒为单位)。

UserId当然,每个日期会有很多条目。 换句话说,用户在一天内将从可用到会议到可用到午餐再到可用等。

我想要做的是以秒为单位的总时间UserIdStatusKey每个日历日。所以它看起来像这样:

cfrinco      3456   Meeting       '2012-01-01'
cfrinco      45678  Available     '2012-01-01'
cfrinco      7845   Meeting       '2012-01-02'
cfrinco      32541  Available     '2012-01-02'
cfrinco      4455   Meeting       '2012-01-03'
cfrinco      12456  Available     '2012-01-03'
fburger      3456   Meeting       '2012-01-01'
fburger      75489  Available     '2012-01-01'
fburger      3541   Lunch         '2012-01-01'
fburger      5486   Meeting       '2012-01-02'
fburger      64587  Available     '2012-01-02'
fburger      3614   Lunch         '2012-01-02'
fburger      3225   Meeting       '2012-01-03'
fburger      35414  Available     '2012-01-03'
fburger      3745   Lunch         '2012-01-03'

非常感谢您的帮助。

4

2 回答 2

1

如果您想要按用户 ID、状态和日期分组的数据,请执行以下操作:

SELECT
 UserId
 SUM(StateDuration),
 StatusKey,
 StatusDateTime
FROM AgentActivityLog
WHERE StatusDateTime BETWEEN '2012-01-01' AND '2012-12-31'
GROUP BY
 UserId,
 StatusDateTime,
 StatusKey
于 2012-12-25T20:58:35.610 回答
0

这就是我一直在寻找的。在对stackoverflow中的不同答案进行一些研究并将它们放在一起之后,我明白了。

SELECT
UserId, SUM(StateDuration) SumStateDuration, StatusKey,
DATEADD(dd, 0, DATEDIFF(dd, 0, StatusDateTime)) StatusDate
FROM [AgentActivityLog]
WHERE StatusDateTime BETWEEN '2012-01-01' AND '2012-12-31'

GROUP BY
 UserId,
 DATEADD(dd, 0, DATEDIFF(dd, 0, StatusDateTime)),
 StatusKey

关键部分是(a)从日期时间列中获取日期,(b)按它分组。

于 2012-12-26T00:09:18.603 回答