Reputation: 342
UPDATE: https://www.db-fiddle.com/f/5fGUTSsAhGRPYPk33wDSzz/0
Sorry for asking the question very similar to the previous one, but I am really stuck here.
There are multiple tables:
Structure:
I am trying to add role fields to items, it should take role_type and it is value from items_zones and if it is NULL fetch fallback value from zone (roles_zones).
I started with:
SELECT
items.id,
(z_roles.role_type_id) as z_role_type_id,
(z_roles.id) as z_role_id,
MAX(i_roles.role_type_id) as i_role_type_id,
MAX(i_roles.id) as i_role_id
FROM
items
LEFT JOIN
zones as j_zones ON j_zones.id = items.zone_id
LEFT JOIN
roles_zones ON roles_zones.zone_id = j_zones.id
LEFT JOIN
roles as z_roles ON (z_roles.id = roles_zones.role_id)
LEFT JOIN
items_roles ON items_roles.item_id = items.id
LEFT JOIN
roles as i_roles ON items_roles.role_id = i_roles.id
AND (z_roles.role_type_id = i_roles.role_type_id)
WHERE
items.id = 834
GROUP BY
items.id, z_roles.role_type_id, z_roles.id
ORDER BY
i_role_id;
Looks right:
id |z_role_type_id |z_role_id |i_role_type_id |i_role_id |
----+---------------+----------+---------------+----------+
834 |5 |111 |5 |68 |
834 |11 |120 |11 |120 |
834 |7 |77 | | |
834 |2 |2 | | |
834 |12 |91 | | |
834 |4 |78 | | |
834 |8 |36 | | |
And now this query:
SELECT
items.id,
z_roles.role_type_id as z_role_type_id,
z_roles.id as z_role_id,
MAX(i_roles.role_type_id) AS i_role_type_id,
MAX(i_roles.id) AS i_role_id,
MAX(CASE
WHEN (i_roles.role_type_id = 5) THEN i_roles.id
WHEN (z_roles.role_type_id = 5) THEN z_roles.id
END) AS role_type_5_value,
MAX(CASE
WHEN (i_roles.role_type_id = 11) THEN i_roles.id
WHEN (z_roles.role_type_id = 11) THEN z_roles.id
END) AS role_type_11_value,
MAX(CASE
WHEN (i_roles.role_type_id = 7) THEN i_roles.id
WHEN (z_roles.role_type_id = 7) THEN z_roles.id
END) AS role_type_7_value
FROM
items
LEFT JOIN
zones AS j_zones ON j_zones.id = items.zone_id
LEFT JOIN
roles_zones ON roles_zones.zone_id = j_zones.id
LEFT JOIN
roles AS z_roles ON (z_roles.id = roles_zones.role_id)
LEFT JOIN
items_roles ON items_roles.item_id = items.id
LEFT JOIN
roles AS i_roles ON items_roles.role_id = i_roles.id
AND (z_roles.role_type_id = i_roles.role_type_id)
WHERE
items.id = 834
GROUP BY
items.id,
z_roles.role_type_id,
z_roles.id
ORDER BY
items.id, i_role_id;
Generates this:
id | z_role_type_id | z_role_id | i_role_type_id | i_role_id | role_type_5_value | role_type_11_value | role_type_7_value
-----+----------------+-----------+----------------+-----------+-------------------+--------------------+-------------------
834 | 5 | 111 | 5 | 68 | 111 | |
834 | 11 | 120 | 11 | 120 | | 120 |
834 | 7 | 77 | | | | | 77
834 | 2 | 2 | | | | |
834 | 12 | 91 | | | | |
834 | 4 | 78 | | | | |
834 | 8 | 36 | | | | |
(7 rows)
Multiple rows and wrong value for role_type_5_value
. Probably because of MAX aggregator. Is it possible to use something like first
aggregator (because rows ordered by i_role_id and first results are right)?
I want this:
id | role_type_5_value | role_type_11_value | role_type_7_value
-----+-------------------+--------------------+-------------------
834 | 68 | 120 | 77
I tried to group by
by aggregated fields, (role_type_5_value
, role_type_11_value
, role_type_7_value
) but this is simply not working.
Upvotes: 1
Views: 62
Reputation: 44230
First: removing the unneeded bridge-tables from the main query and squeeze them into EXISTS()
terms will simplify your query.(you only need three tables, the rest is glue)
Second: don't put all your terms in the GROUP BY
clause.
SELECT
i0.id
, MAX(CASE
WHEN (r1.role_type_id = 5) THEN r1.id
WHEN (r0.role_type_id = 5) THEN r0.id
END) AS role_type_5_value
, MAX(CASE
WHEN (r1.role_type_id = 11) THEN r1.id
WHEN (r0.role_type_id = 11) THEN r0.id
END) AS role_type_11_value
, MAX(CASE
WHEN (r1.role_type_id = 7) THEN r1.id
WHEN (r0.role_type_id = 7) THEN r0.id
END) AS role_type_7_value
FROM items i0
LEFT JOIN roles AS r0
ON EXISTS ( SELECT*
FROM zones AS z0
JOIN roles_zones rz ON rz.zone_id = z0.id
WHERE z0.id = i0.zone_id
AND r0.id = rz.role_id)
LEFT JOIN roles AS r1
ON EXISTS ( SELECT*
FROM items_roles ir
WHERE ir.item_id = i0.id
AND ir.role_id = r1.id
AND r0.role_type_id = r1.role_type_id
)
WHERE i0.id = 834
GROUP BY i0.id
-- r0.role_type_id,
-- r0.id
ORDER BY i0.id;
Upvotes: 1