I have a table with accidents (~110,000 records) and a table with municipalities (~400 records). All accidents have a municipality code of the municipality where they occurred and I can count the number of accidents per municipality. For my project I need numbers / municipality.
One of the columns of the accidents table contains the type of object against which the accident occurred, e.g. car, truck, bicycle, etc. What I would like is to create new columns for each object type (car, truck, etc) with a count for each municipality.
Edit
Example of accidents table
mun_code objecttype
1 car
1 truck
1 car
2 bicycle
2 car
2 bicycle
2 truck
3 tree
Desired output
mun_code car truck bicycle tree
1 2 1 null null
2 1 1 2 null
3 null null null 1
Instead of null, 0 is ok.
Edit 2
The point is that I have many columns that I have to aggregate over municipalities. I just wonder whether it is possible to have a script that automatically creates the needed columns for each objecttype and automatically aggregates for each objecttype
Is this possible to do in (postgres) SQL and how should I do this?