Skip to content
Advertisement

Count frequency of values in rows and columns SQL

I have an SQLite database and I am trying to count the occurrence of values in a column but also values in a corresponding row.

My dataset is:

and the expected output would be something like

I am able to get the number of games played with the query

But I can’t seem to figure out how to count the number of times win = 1 compared to the champion.

Advertisement

Answer

You can do conditional aggregation:

This works because win contains 0s and 1s only. If it was a string like 'won', 'lost', you would do:

Side note: do not use single quotes for identifiers. Although some databases may accept it, single quotes should be reserved for literal strings only. Use the proper quoting character for your database (in SQLite, double quotes are preferred), or better yet use identifiers that do not require quoting.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement