With a table like:
name country state
----- ------- -----
ana usa ca
paul usa wy
louis can ot
ana usa ca
john can al
paul usa wy
john usa ca
ana usa ca
louis can ot
I need a result like:
country state ana paul louis john
-------- ----- ---- ---- ----- ----
usa ca 3 1 0 1
wy 0 1 0 0
can ot 0 0 2 0
al 0 0 0 1
(count occurrences by name with dynamic columns, group by country and state)
Is it possible to build it with just one sentence?
You need pivot operation to achieve the result.
http://sqlfiddle.com/#!3/aae09/3