I am trying to build a PostgreSQL query that does the following, but so far my efforts have been in vain.
Problem:
There are two tables: A and B. I would like to select all the columns from table A (having the columns: id, name, description) and replace the column "A.name" with the value of the column "B.title", from table B ( with columns: id, table_A_id title, langcode), where B.table_A_id is 5, and B.langcode is "nl" (if there are any rows).
My attempts:
SELECT A.name,
case when exists(select title from B where table_A_id = 5 and langcode= 'nl')
then B.title
else A.name
END
FROM A, B
WHERE A.id = 5 and B.table_A_id = 5 and B.langcode = 'nl'
SELECT COALESCE(B.title, A.name) as name
from A, B
where A.id = 5 and B.table_A_id = 5 and exists(select title from B where table_A_id = 5 and langcode= 'nl')
I tried using CASE and COALESCE () but could not because of my inexperience with both concepts.
Thanks in advance.