简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM