views:

192

answers:

2

Cannot get my head around the SQL that will return the number of times a user has accessed a particular service. Think it might require a nested count and select, but cannot get my head around it.

The data looks like this:

UserID  Service
---------------
1       Map1
1       Map2
1       Map1
2       Map1
2       Map2
3       Map4
3       Map2
3       Map2
3       Map2
...     ...

And the desired kind of output is something along the lines of this:

UserID  Service  TimesAccessed
------------------------------
1       Map1     2
1       Map2     1
2       Map1     1     
2       Map2     1
3       Map3     3
3       Map4     1
...     ...      ...

Any help would be much appreciated.

+2  A: 

Something like this?

select  
    userid, service, count(userid)  
 from   
    table  
 group by  
    userid, service
Bravax
Many thanks Bravax
Ali J
No problem, glad we could help.
Bravax
Sorry, think you actually pipped me by a split second as well!
David M
It's fine, this once, don't do it again. Your answer was better formatted and complete though.
Bravax
+5  A: 

I think this does it:

SELECT  UserID, Service, COUNT(UserID) TimesAccessed
FROM    Table
GROUP BY UserID, Service
David M
Brilliant - both answers work. One of those mornings....!
Ali J