Reputation: 4099
Please consider below. I wish to list the number of times that each users made reservations for a conference room, and how many different rooms he/she used:
room user
----------
1 1
2 1
3 2
4 3
4 3
Desired output:
user reservations rooms
-------------------------
1 2 2
2 1 1
3 2 1
Can anyone tell me how to do this?
I now have:
select [user], count(1)
from [table]
group by [user]
But this doesn't give me the rooms count. Adding another count()
doesn't work.
Upvotes: 0
Views: 41
Reputation: 32003
Use aggregation with distinct for room
Select user, count(*) reservation_ numbr,count(distinct room) as rooms
from t group by user
Upvotes: 0
Reputation: 24763
you need a count ( distinct <column> )
for the room
select [user], count(*) as reservations, count(distinct room) as rooms
from [table]
group by [user]
Upvotes: 3