我需要根据“金额”列运行一个关于用户效率的查询。另外,我需要知道输入的第一笔金额的日期/时间。
现有记录:
UserID Amount DateTime
1 1 09/25/2015 08:00:01
1 2 09/25/2015 08:31:11
1 3 09/25/2015 09:02:22
2 2 09/25/2015 09:57:42
3 1 09/25/2015 10:11:12
3 4 09/25/2015 11:10:09
4 1 09/25/2015 12:15:15
4 5 09/25/2015 12:22:56
5 2 09/25/2015 12:57:32
预期产出
UserID TotalAmount FirstDateTime
1 6 09/25/2015 08:00:01
2 2 09/25/2015 09:57:42
3 5 09/25/2015 10:11:12
4 6 09/25/2015 12:15:15
5 2 09/25/2015 12:57:32
我可以通过
获得前两列SELECT UserID, SUM(Amount) As TotalAmount FROM tblAmounts GROUP BY UserID
但是我无法弄清楚我需要做些什么来包含第三列。
答案 0 :(得分:4)
您只需为第三个值添加另一个聚合函数。
SELECT UserID, SUM(Amount) As TotalAmount, min(DateTime) as FirstDateTime FROM tblAmounts GROUP BY UserID