Jump to content

Count with sub querry


Ericc

Recommended Posts

I have this table "plays":  
     
artist title  
artist_1 title_1  
artist_1 title_1  
artist_1 title_2  
artist_2 title_1  
     
I want to have something like this:
     
artist title counts
artist_1 title_1 2
artist_1 title_2 1
artist_2 title_1 1

 

I tried some querries wih SELECT DISTINCT and and GROUP BY but did not get what i wanted.

Do you have any hint for me? I guess i need a sub querry. Thank you!

 

Link to comment
Share on other sites

  • 2 weeks later...

Hey there! Welcome to the forums!

This one can be a bit mind boggling. But I did manage to get it to work.

It involves using GROUP BY on both `artist` and `title`. Take a look!

SELECT *, COUNT(title)
FROM plays
GROUP BY artist, title

 

Link to comment
Share on other sites

  • 3 weeks later...
SELECT 
     SUM(CASE WHEN column1 IS NOT NULL THEN 1 ELSE 0 END) AS column1_count
    ,SUM(CASE WHEN column2 IS NOT NULL THEN 1 ELSE 0 END) AS column2_count
    ,SUM(CASE WHEN column3 IS NOT NULL THEN 1 ELSE 0 END) AS column3_count
FROM table

 

 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...