Ashish Chaturvedi Ashish Chaturvedi - 1 year ago 81
SQL Question

How to shift column name to result set in MySQL?

I have a query

count(CASE WHEN fan_gate.genre = 'Breaks' THEN END) AS 'Breaks',
count(CASE WHEN fan_gate.genre = 'Cill Out' THEN END) AS 'Chill Out',
count(CASE WHEN fan_gate.genre = 'Deep House' THEN END) AS 'Deep House',
count(CASE WHEN fan_gate.genre = 'Drum & Bass' THEN END) AS 'Drum & Bass'
FROM fan_gate

Current result set

Breaks | Cill Out |Deep House |Drum & Bass
4581 | 785 | 7858 | 4474

I need

Column 1 | column 2
Breaks | 4581
Cill Out | 785
Deep House | 7858
Drum & Bass | 4474

Answer Source

You just want group by:

SELECT fg.genre, COUNT(*)
FROM fan_gate fg
GROUP BY fg.genre;

If you only care about those four genres, then add a WHERE clause before the GROUP BY to select the ones you care about.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download