使用GROUPBY进行SUM查询

时间:2014-07-05 08:13:30

标签: sql tsql

我有这样的表:

___________________________________________________________
| Date(date)  | UserID(int)  | Enter(time)  | Leave(time) |
|_____________|______________|______________|_____________|
|'2000-01-01' | 1            | 07:00:00     | 10:00:00    |
|'2000-01-01' | 2            | 10:00:00     | 12:00:00    |
|'2000-01-02' | 1            | 07:00:00     | 12:00:00    |
|'2000-01-02' | 2            | 12:00:00     | 18:00:00    |
|'2000-01-03' | 1            | 07:00:00     | 11:00:00    |
|'2000-01-03' | 2            | 11:00:00     | 13:00:00    |

我希望得到如下结果:

__________________________________________
| UserName(varchar(50)) | Summary(float) |
|_______________________|________________|
| User1                 | 12             |
| User2                 | 10             |

我想在Table1.UserID = Users.UserName

上使用Users表加入My Table1表

你能帮帮我吗?

- 编辑:

我试过arleady ......就像:

SELECT Entries.UserID, CAST(DATEDIFF(MINUTE,Entries.Enter, Entries.Leave) AS float)/60 AS Hours
                            FROM Entries
                            GROUP BY Entries.UserID

但是它还没有处理UserName,而且它不起作用

- 编辑2:

我创建的查询如下:

SELECT   Users.UserName,
             SUM(CAST(DATEDIFF(MINUTE, Enter, Leave) as float)) / 60 AS Summary
    FROM Entries  
    RIGHT JOIN Users  ON Entries.UserID = Users.Id
    WHERE ((1=1 AND @StartDate IS NULL) OR (@StartDate is NOT NULL AND Entries.Date >= @StartDate)) AND
          ((1=1 and @EndDate is NULL) OR ( @EndDate is NOT NULL AND Entries.Date <= @EndDate))                       
    GROUP BY Users.UserName

但是选择所有用户似乎有问题。当我删除WHERE语句时,它的工作。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

解决方案看起来像这样;

;WITH MyTable ([Date], UserID, Enter, Leave) AS
(
    SELECT '2000-01-01', 1, '07:00:00', '10:00:00' UNION ALL
    SELECT '2000-01-01', 2, '10:00:00', '12:00:00' UNION ALL
    SELECT '2000-01-02', 1, '07:00:00', '12:00:00' UNION ALL
    SELECT '2000-01-02', 2, '12:00:00', '18:00:00' UNION ALL
    SELECT '2000-01-03', 1, '07:00:00', '11:00:00' UNION ALL
    SELECT '2000-01-03', 2, '11:00:00', '13:00:00' 
)
SELECT   MT.UserID
        ,MT.Summary = SUM(DATEDIFF(MINUTE, Enter, Leave)) / 60.
        ,US.OtherUserField
FROM MyTable    MT
JOIN Users      US ON MT.UserID = US.UserID
GROUP BY UserID, US.OtherUserField

关于你的编辑2;

SELECT   Users.UserName
        ,Summary    = SUM(CAST(DATEDIFF(MINUTE, Enter, Leave) as float)) / 60 
FROM Entries  
RIGHT 
JOIN Users      ON  Entries.UserID = Users.Id
                AND ((@StartDate IS NULL) OR (Entries.Date >= @StartDate)) 
                AND ((@EndDate is NULL) OR (Entries.Date <= @EndDate))                       
GROUP BY Users.UserName

答案 1 :(得分:1)

您需要将WHERE条件移动到ON子句,否则结果与内部联接相同。

你也应该在加入之前聚合(通常表现更好):

SELECT   Users.UserName,
         e.Summary
FROM Users as a
left join 
 (
   select UserId,
      SUM(CAST(DATEDIFF(MINUTE, Enter, Leave) as float)) / 60 AS Summary
   from Entries
   WHERE (@StartDate IS NULL OR Entries.Date >= @StartDate) 
     AND (@EndDate is NULL   OR Entries.Date <= @EndDate)                       
   group by UserId
 ) as e  
ON e.UserID = Users.Id