How can I show the grouped column into a single row to make it visually clear with the count value? I think there must be a concise way instead of joining multiple table to show a single row.
My table (tb) looks like this
Name Subjects
David Geography
David Geography
Sherry History
Paul History
Paul Math
Ken Chemistry
I want to show the table like this
Name Geography History Math Chemistry Total
David 2 0 0 0 2
Sherry 0 2 0 0 2
Paul 0 1 1 0 2
Ken 0 0 0 1 1
I'm doing like multiple join tables
SELECT g.name, g.count, h.count, m.count, c.count,
g.count + h.count + m.count + c.count as Total
FROM
(SELECT name, count(*) as count
FROM tb
WHERE Subjects = "Geography"
GROUP BY Name) as g
JOIN
(SELECT name, count(*) as count
FROM tb
WHERE Subjects = "History"
GROUP BY Name) as h
ON g.name = h.name
JOIN
(SELECT name, count(*) as count
FROM tb
WHERE Subjects = "Math"
GROUP BY Name) as m
ON g.name = m.name
JOIN
(SELECT name, count(*) as count
FROM tb
WHERE Subjects = "Chemistry"
GROUP BY Name) as c
ON g.name = c.name