I made a table called tbl
with this code:
CREATE TABLE tbl
(
`Year` int,
`Album` varchar(255),
`Artist` varchar(255),
`Label` varchar(255),
`Genre` varchar(255),
`id` int
)
;
INSERT INTO tbl
(
`Year`,
`Album`,
`Artist`,
`Label`,
`Genre`,
`id`
)
VALUES
(1990, "Greatest Hits", "The Best", "Least Def", "hip hop", 123),
(1990, "Greatest Hits", "The Best", "Roofless", "hip hop", 123),
(1990, "4-Boyz", "3 Guyz", "Pacific", "pop-dance", 23),
(1990, "4-Boyz", "3 Guyz", "Atlantic", "pop-dance", 23)
;
I want to run a query to show me the count of genres for each year, without double counting because of the Label
column. I want this:
Year, hip hop, pop-dance
1990, 1, 1
What query must I run to get what I want?
Because you can't use
pivot
, you can do this.The accepted answer worked for me.
I add here a more complex case, with join, in case someone needs.
see also:
Combining 2 SQL queries and getting result set in one
my example: