I have a table:
Rank | Title | Genre |
---|---|---|
1 | Guardians of the Galaxy | Action,Adventure,Sci-Fi |
2 | Prometheus | Adventure,Mystery,Sci-Fi |
3 | Split | Horror,Thriller |
4 | Sing | Animation,Comedy,Family |
5 | Suicide Squad | Action,Adventure,Fantasy |
Is there any function that I can count how many distinct text contain in theses columns? And how many columns contain each text?
I am using MYSQL
Like
Total distinct text |
---|
10 |
And
Adventure | Sci-Fi | Mystery | Horror | Thriller | Animation | Family | Comedy | Fantasy |
---|---|---|---|---|---|---|---|---|
2 | 3 | 2 | 1 | 1 | 1 | 1 | 1 | 1 |
2
Answers
I’m updated my answer since you updated your question. In SQL tables, it’s good practice to separate values. Your Genre column contains multivalued attributes. It will be easier to count how many movies each Genre contains with that way, or create another table.
Also, I think the total distinct test is supposed to be 5 since you’re counting the distinct titles. You might want to use the keyword
DISTINCT
in SQL for that.To Split comma separated string into rows concatenate square brackets ([]) around your string to make it into a JSON array. Then use JSON_TABLE to convert it into a table.
To count distinct text use this query :
To count per genre :
You can use the conditional aggregation to Pivot rows into columns :
Demo here