user3218088
user3218088

Reputation: 253

Complex SQL Query Using Group By

I have a table like this:

User   Referred By
A      FB
B      Google
C      Twitter
A      FB
B      Friend
D      FB
E      FB
B      Friend
A      Friend

I am interested in output like:

User referred_by count  
A    FB          2
A    Friend      1
B    Friend      2
B    Google      1

and so on.

I have made several queries but not able to get this as my output, I am a beginner in SQL.

Upvotes: 0

Views: 47

Answers (1)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Try this, maybe with some modifications depending in your RDBMS. The User is reserved word in must RDBMS, you must manage it accordingly.

SELECT User, ReferredBy, COUNT(*) cnt
FROM tbl
GROUP BY User, ReferredBy

Upvotes: 3

Related Questions