I have the following table and i am supposed to count the entries(5,4,3,2,1) per column name
ID | ClientID | Responsiveness1 | Responsiveness2 | Reliability1 |
---|---|---|---|---|
1024 | 3511 | 5 | 4 | 3 |
1025 | 4571 | 5 | 3 | 5 |
1026 | 3827 | 4 | 5 | 4 |
1027 | 7652 | 1 | 1 | 1 |
1028 | 7778 | 2 | 2 | 2 |
1029 | 7612 | 1 | 1 | 2 |
I tried counting it manually (Select Count(Responsiveness1) from table where Responsiveness1 = ‘5’) by column name and by the range (5,4,3,2,1) and got the output. Unfortunately though, I have to count it per column and per range then display it on my table.
Is there an easier and more efficient way to do it to have an output of:
Rate | Responsiveness1 | Responsiveness2 | Reliability1 |
---|---|---|---|
5 | 2 | 1 | 1 |
4 | 1 | 1 | 1 |
3 | 0 | 1 | 1 |
2 | 1 | 1 | 2 |
1 | 2 | 2 | 1 |
Instead of having to count it by column name and then inserting it in the output table?
2
Answers
You can join your table with a table of rates and count entries conditionally
db<>fiddle
You may use
full join emulation
for MySql thruunion all
and then"pivot"
table. This case is usable for an indefinite set ofrate
values.I’ll add Pivot result example
Table
What case is suitable in a specific case – customer request or developer preferences:)