I have a table in Postgres with an hstore column like:
"surface => concrete, ramp => yes, incline => up, highway => footway"
"surface => asphalt, lit => no, source => survey, incline => 12.6%, highway => footway"
"bicycle => yes, surface => asphalt, highway => footway, segregated => no"
Now I would like to analyse the keys among themselves. Therefore I want to know for the whole dataset how often the keys occur in combination pairwise.
The result should look something like this:
| Key 1 | Key 2 | Count |
|---------------------|------------------|------------------|
| surface | source | 1 |
| surface | highway | 3 |
| surface | incline | 2 |
| highway | bicycle | 1 |
.....
2
Answers
Use hstore’s
each()
to expand it out, then join and aggregate:fiddle
Use the function
akeys()
to get all keys of thehstore
column as an array, generate all distinct pairs of the arrays elements withgenerate_subscripts()
and group the result by the pairs:Test it in db<>fidlle.