I have a table entity_translations
, which relate O->M to string_translations
via entity_translation_fields
. Each row in string_translations
has an additional status
that can be obtained via a join with current_string_translation_status
.
I need to add an additional column to the result of a select from entity_translations
that will be determined based on the following conditions:
- If all
string_translation
records that correspond to anentity_translation
record havestatus
='READY'
, then thatentity_translation
gets status'READY'
. - Else, if any
string_translation
records that correspond to anentity_translation
record havestatus
='FAILED'
, then thatentity_translation
gets status'FAILED'
. - Else, if any
string_translation
records that correspond to anentity_translation
record havestatus
='IN_PROGRESS'
, then that entity translation gets status'IN_PROGRESS'
. - Else, that entity translation gets status
'ADDED'
.
What I want
Given schema and data in this fiddle, I would like to have the following result:
| id | entity_id | entity_type | status |
| --- | --------- | ----------- | ----------- |
| 1 | 1 | post | FAILED | <--- Because status for string translation 2 is "FAILED"
| 2 | 2 | post | IN_PROGRESS | <--- Because status for string translation 5 is "IN_PROGRESS"
| 3 | 3 | post | READY | <--- Because status for string translation 6 is "READY"
Here's the query I used to get the data that I would like to aggregate into the above desired result:
SELECT *
FROM `entity_translations`
JOIN `entity_translation_fields`
ON (`entity_translation_fields`.`entity_translation_id` = `entity_translations`.`id`)
JOIN `string_translations`
ON (`string_translations`.`id` = `entity_translation_fields`.`string_translation_id`)
JOIN `current_string_translation_status`
ON (`current_string_translation_status`.`string_translation_id` = `string_translations`.`id`)
ORDER BY `entity_translations`.`id` ASC, `string_translations`.`id` ASC;
Markdown tables weren't rendering for some reason, so I just added it as code.
What I tried
- One way could be to join
entity_translation
withstring_translation
(and its status view), then group byentity_translation
's PK. But I don't know how I could then determine the values from thestatus
column that is not inGROUP BY
. IfANY()
andALL()
were aggregate functions, I could use them in multiple nestedIF
conditions; but they're not. - Perhaps, another way is to use a subquery to add a column to
string_translation
. But I don't understand how to then useANY
orALL
with yet another subquery.
Here is the whole setup. It has some extra things that are not relevant to this current problem. The query that is relevant is the one that says the following on top:
-- Get data for all entity translations, including their current status <----- PROBLEM
Best Answer
If I understand the task correctly, then
fiddle