I have a four-part linked server query that LEFT OUTER JOINs a table created on the fly using UNION ALL, something along these lines:
SELECT t.column1, rlt.s6, SUM(t.column3) column3
FROM Linked_server.database.schema.table t
LEFT OUTER JOIN (
SELECT 1 AS s7, 15 AS s6
UNION ALL SELECT 3, 1
UNION ALL SELECT 20, 16
) rlt ON (rlt.s7 = t.column2)
That works fine. However, when I'm running that same query with OPENQUERY function, like that:
SELECT * FROM OPENQUERY(Linked_server, '
SELECT t.column1, rlt.s6, SUM(t.column3) column3
FROM database.schema.table t
LEFT OUTER JOIN (
SELECT 1 AS s7, 15 AS s6
UNION ALL SELECT 3, 1
UNION ALL SELECT 20, 16
) rlt ON (rlt.s7 = t.column2)
')
, LEFT OUTER JOIN fails and s6 column only contains NULLs. I understand I could rewrite the query #2 to make it work, but I am curious why it doesn't work the way it is written? Is it because there is some limitation on OPENQUERY I am unaware of?