Hi guys,
I am trying to write a SQL query where I am joining two tables and retrieving a few columns. Once this is done, based on a two fields (source table enum, id in the corresponding source table), I need to retrieve a value matching the id in the source table.
The issue is that I cannot join all the source tables, and want to do something like this:
Select
X.Col1,
X.Col2,
Y.Col1,
Y.Col2,
CASE
WHEN Y.TableID = 4 THEN Select t4.Col1 FROM TableFour t4 WHERE t4.id = Y.FileID
WHEN Y.TableID = 5 THEN Select t5.Col4 FROM TableFive t5 WHERE t5.id = Y.FileID
END
FROM Table X INNER JOIN Table Y ON X.ID = Y.XID
I can guarantee the value being retrieved from all the source tables will be the same (i.e. nvarchar).
Nesting SQL queries inside the CASE statement doesn't seem to work, I am throwing it you guys. Any ideas with this problem?
Hope I explained the question adequately. If you are unsure, make a comment so I can clarify it.
Cheers in advance!