I am working on a table which looks something like this:
user_id | key | scope | value
--------+-------------+-----------+-------
1 | someSetting | user | false
1 | someSetting | group | true
1 | someSetting | company | false
2 | someSetting | user | false
2 | someSetting | group | true
3 | someSetting | user | false
4 | someSetting | group | true
The settings are in a hierarchy: company -> group -> user, with the user overriding the group which in turn overrides the company. When querying by user_id, I want to effectively merge the settings by this hierarchy, if it exists. For the above sample, I want to see this as the result:
user_id | key | value
--------+-------------+-------
1 | someSetting | false
2 | someSetting | true
3 | someSetting | false
4 | someSetting | true
I am currently doing the merge operation after the rows are retrieved from Postgres, but it would be more efficient if this can be done in the query itself. I looked at aggregate functions, but doesn't look like any of them fit my requirement.
This seems simple enough that I'm sure it can be done using Postgres. Any pointers appreciated!