sql server - Including a single value when grouping in SQL -


i need run query on how productive user has been, based on "amount" column. in addition, need know date/time of first amount entered.

existing records:

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 

expected output

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 

i can first 2 columns doing

select userid, sum(amount) totalamount tblamounts group userid 

but having trouble figuring out need in order include third column.

you can add aggregate function third value.

select userid, sum(amount) totalamount, min(datetime) firstdatetime  tblamounts group userid 

Comments