ItHurtsWhenIThink
Technical User
I need to group by an aggragate function. I have a bunch of users and I need to add up all their points, whether or not they have any. Need to list all users (and only distinct), then I need to sort by Points, Last Name.
Here is what I have. It has two problems. Unable to get all users and can not sort (group by) points.
Markros was a great help on this, but I was never able to find a solution.
SELECT DISTINCT(LeaveRequests.TakenBy),SUM(Leave_Points.Points) AS MaxPoints, SUM(LeaveRequests.Hours) AS tltHours, LeaveRequests.LeaveDate,Users.DeptID
FROM Leave_Points INNER JOIN
LeaveRequests ON Leave_Points.LeaveID = LeaveRequests.LeaveID AND LeaveRequests.LeaveDate>'1/1/2005' RIGHT OUTER JOIN
Users ON Leave_Points.UserID = Users.UserID
WHERE (Users.DeptID = '1')
Group BY Hours,TakenBy,LeaveDate,Users.DeptID
RESULT:
UserID Points Hours LeaveDate DeptID
NULL NULL NULL NULL 1
7 1.0 24 2009-04-01 00:00:00 1
8 0.0 4 2009-02-26 00:00:00 1
113 1.0 24 2009-04-15 00:00:00 1
624 0.5 12 2009-02-23 00:00:00 1
624 0.5 12 2009-03-11 00:00:00 1
624 0.5 12 2009-03-17 00:00:00 1
643 1.0 24 2009-04-21 00:00:00 1
644 0.5 13 2009-02-27 00:00:00 1
645 1.0 24 2009-03-05 00:00:00 1
648 0.5 12 2009-03-18 00:00:00 1
656 1.0 24 2009-02-26 00:00:00 1
656 1.0 24 2009-04-23 00:00:00 1
657 1.0 24 2009-02-26 00:00:00 1
Any thought?
Here is what I have. It has two problems. Unable to get all users and can not sort (group by) points.
Markros was a great help on this, but I was never able to find a solution.
SELECT DISTINCT(LeaveRequests.TakenBy),SUM(Leave_Points.Points) AS MaxPoints, SUM(LeaveRequests.Hours) AS tltHours, LeaveRequests.LeaveDate,Users.DeptID
FROM Leave_Points INNER JOIN
LeaveRequests ON Leave_Points.LeaveID = LeaveRequests.LeaveID AND LeaveRequests.LeaveDate>'1/1/2005' RIGHT OUTER JOIN
Users ON Leave_Points.UserID = Users.UserID
WHERE (Users.DeptID = '1')
Group BY Hours,TakenBy,LeaveDate,Users.DeptID
RESULT:
UserID Points Hours LeaveDate DeptID
NULL NULL NULL NULL 1
7 1.0 24 2009-04-01 00:00:00 1
8 0.0 4 2009-02-26 00:00:00 1
113 1.0 24 2009-04-15 00:00:00 1
624 0.5 12 2009-02-23 00:00:00 1
624 0.5 12 2009-03-11 00:00:00 1
624 0.5 12 2009-03-17 00:00:00 1
643 1.0 24 2009-04-21 00:00:00 1
644 0.5 13 2009-02-27 00:00:00 1
645 1.0 24 2009-03-05 00:00:00 1
648 0.5 12 2009-03-18 00:00:00 1
656 1.0 24 2009-02-26 00:00:00 1
656 1.0 24 2009-04-23 00:00:00 1
657 1.0 24 2009-02-26 00:00:00 1
Any thought?