I’m using SQL Server Express and I’m trying to pull different columns from different tables using LEFT OUTER JOIN. It’s working great but only if all columns exist. So I’ve been reading for the past hour how to add a condition so that LEFT OUTER JOIN is done only if column exists.
See code below ( the issue is the last LEFT OUTER JOIN because a.[Page Path] doesn’t exist):
SELECT
b.[Page ID],
ISNULL(b.[Page Group],'Other Landing Page') AS [Landing Page Group],
ISNULL(c.[Page Group],'Other Second Page') AS [Second Page Group],
ISNULL(d.[Page Group],'Other Page') AS [Page Path Group],
a.*
FROM [mychoice-data-b9BwZvd] a
LEFT OUTER JOIN [mychoice-pagedims] b
ON
(a.[Landing Page Path] LIKE b.[Page ID])
LEFT OUTER JOIN [mychoice-pagedims] c
ON
(a.[Second Page Path] LIKE c.[Page ID])
LEFT OUTER JOIN [mychoice-pagedims] d
ON
a.[Page Path] LIKE d.[Page ID]
WHERE a.[Page Path] IS NOT NULL
I’ve tried IF(EXISTS, but whatever I do I get an error ‘Invalid column name ‘Page Path”.
I don’t think that you can do that in a single query, a way I suggest you to use is something like this: