-2

我的查询有问题,我希望它返回我的结果集为 Year = 2013, Month = 1 但每个用户每个月只有一行,当月的登录时间总计,我认为问题是与我的分组:

SELECT  YEAR(UP_Date) AS 'Year',
        MONTH(UP_Date) AS 'Month',
        SUM(ISNULL(UP_UserID,0)) AS UserID,
        U_UserName AS UserName,
        UP_AveCallTime AS AveCallTime,
        UP_TotCallTime AS TotCallTime,
        CAST(DATEDIFF(DAY,UP_LogOn, UP_LogOff) AS varchar) + ':' +
        CAST(DATEDIFF(HOUR,UP_LogOn, UP_LogOff) AS varchar) + ':' +
        CAST(DATEDIFF(MINUTE,UP_LogOn,UP_LogOff) AS varchar) AS [HoursOfMonth]

FROM [RPT_User_Performance_Data_cus]
INNER JOIN Users us ON
    UP_UserID = us.ID

GROUP BY Year(UP_Date), MONTH(UP_Date), UP_AveCallTime, UP_TotCallTime, U_UserName, UP_LogOn, UP_LogOff

------Result I Get----------------------------
Year    Month   UserID  UserName    AveCallTime TotCallTime HoursOfMonth
2013    6   18552   BossJobs    NULL               NULL           0:0:4
2013    6   18552   BossJobs    NULL               NULL           0:0:02
2013    6   18552   BossJobs    NULL               NULL           0:3:21
2013    6   18552   BossJobs    NULL               NULL           0:5:01
2013    6   18552   BossJobs    NULL               NULL           NULL
2013    6   18553   KingGates   NULL               NULL           0:0:2
2013    6   18553   KingGates   NULL               NULL           0:1:31

-------Instead of ---------------------
Year    Month   UserID  UserName    AveCallTime TotCallTime HoursOfMonth
2013    6   18552   BossJobs    NULL               NULL            0:5:24
2013    6   18553   KingGates   NULL               NULL            0:1:33
4

2 回答 2

1

如果我能理解你的询问,

您按您不想要的事物进行分组,例如UP_AveCallTime, UP_TotCallTime, U_UserName, UP_LogOn, UP_LogOff,因此将它们从group by

对于您AveCallTime,TotCallTime,HoursOfMonth将要使用SUMAVG取决于您的逻辑

于 2013-07-22T14:48:25.753 回答
0

尝试这样的事情:

SELECT  YEAR(UP_Date) AS 'Year',
        MONTH(UP_Date) AS 'Month',
        MAX(ISNULL(UP_UserID,0)) AS UserID,
        U_UserName AS UserName,
        AVG(UP_AveCallTime) AS AveCallTime,
        SUM(UP_TotCallTime) AS TotCallTime,
        CAST(SUM(DATEDIFF(DAY,UP_LogOn, UP_LogOff)) AS varchar) + ':' +
        CAST(SUM(DATEDIFF(HOUR,UP_LogOn, UP_LogOff)) AS varchar) + ':' +
        CAST(SUM(DATEDIFF(MINUTE,UP_LogOn,UP_LogOff)) AS varchar) AS [HoursOfMonth]
FROM [RPT_User_Performance_Data_cus]
INNER JOIN Users us ON UP_UserID = us.ID
GROUP BY Year(UP_Date), MONTH(UP_Date), U_UserName

不确定你的逻辑得到[HoursOfMonth]. 您可能需要对其进行调整或更好地解释。

于 2013-07-22T15:07:00.260 回答