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 Answers 2

9

I think this does it:

SELECT  UserID, Service, COUNT(UserID) TimesAccessed
FROM    Table
GROUP BY UserID, Service
Sign up to request clarification or add additional context in comments.

Comments

2

Something like this?

select  
    userid, service, count(userid)  
 from   
    table  
 group by  
    userid, service  

3 Comments

No problem, glad we could help.
Sorry, think you actually pipped me by a split second as well!
It's fine, this once, don't do it again. Your answer was better formatted and complete though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.