Sql query to Count 0s and 1s with Case #sql #database #sqltips #interview #databaseconcepts #views

Details
Title | Sql query to Count 0s and 1s with Case #sql #database #sqltips #interview #databaseconcepts #views |
Author | Desk For Dev |
Duration | 0:05 |
File Format | MP3 / MP4 |
Original URL | https://youtube.com/watch?v=Ty4AHSWO3KE |
Description
COUNT(CASE WHEN value = 0 THEN 1 END): Counts rows where value is 0. The CASE statement returns 1 when the condition is true (i.e., value = 0), and NULL otherwise. COUNT only counts non-NULL values, so this effectively counts the number of 0s.
COUNT(CASE WHEN value = 1 THEN 1 END): Similarly, counts rows where value is 1.