The table has columns English, Spanish and French. Each with yes or no values and i want to count the number of yes and no for each column into one table.
I tried using distinct count but it cannot put all columns under one table.
It should appear like this:
Key| French | Spanish |English|
--—| -------- | ------- |——————-|
Yes| 45 | 35 | 72 |
No | 27 | 37 | 0 |
3
Answers
You could try a
GROUP BY
clause.Using the following schema
Assuming that your table that has the three columns English, Spanish and French is called Source_Table, here’s a query that does what you want:
Hope this helps.
Best regards.
It is rather uncommon to use a string column for ‘yes’ and ‘no’ instead of a boolean column in MySQL, but well, the query is almost the same here.
You want one result row for ‘Yes’ and one for ‘No’, but each row has a mix of yes and no values. So, you need a
UNION ALL
for the two result rows. The two queries you are unioning go throgh all rows and count the languages conditionally. In standard SQL we would useCOUNT(*) FILTER (WHERE french = 'yes')
for this, in some DBMS that don’t support theFILTER
clause, we’d useCOUNT(CASE WHEN french = 'yes' THEN 1 END)
orSUM(CASE WHEN french = 'yes' THEN 1 ELSE 0 END)
, but in MySQL this gets even simpler, as true = 1 and false = 0 in MySQL, and we can use a mereSUM(french = 'yes')
.The complete query: