I want to know number of column1 value group by column2 and total count in mysql 5.5 . below example will help to understand my problem:
-------------------
column1 | column2
-------------------
of | d1
of | d1
sf | d2
sf | d3
sf | d3
tf | d2
tf | d3
tf | d1
of | d1
-------------------
the output will be:
------------------------------
column2 | of | sf | tf | total
-------------------------------
d1 | 3 | 0 | 1 | 4
d2 | 0 | 1 | 1 | 2
d3 | 0 | 2 | 1 | 3
------------------------------
I searched but didn't found exact solution for my scenario. I can do it with RANK but 5.5 will not support RANK function.
You can do it with conditional aggregation:
select column2,
sum(column1 = 'of') of,
sum(column1 = 'sf') sf,
sum(column1 = 'tf') tf,
count(*) total
from tablename
group by column2
See the demo .
Results:
| column2 | of | sf | tf | total |
| ------- | --- | --- | --- | ----- |
| d1 | 3 | 0 | 1 | 4 |
| d2 | 0 | 1 | 1 | 2 |
| d3 | 0 | 2 | 1 | 3 |
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.