Hi.Can I optimize this query? :SELECT A.Col1, A.Col2, B.Col1, CASE WHEN A.Col3 IS NULL THEN (SELECT TOP 1 C.Col4 FROM dbo.TableC C WHERE C.Col1 = A.Col1) ELSE (SELECT TOP 1 D.Col3 FROM dbo.TableD D) END AS Value1, ISNULL(B.Col2, (SELECT TOP 1 C.Col4 FROM dbo.TableC C WHERE C.Col1 = A.Col1)) AS Value2FROM dbo.TableA A, dbo.TableB B WHERE A.Col5 = B.Col6
I would like to not repeat twice this subquery: (SELECT TOP 1 C.Col4 FROM dbo.TableC C WHERE C.Col1 = A.Col1)
.I think I've seen somewhere article that is possible to write a subquery "before" the main query and then use it value again and again... or am I wrong?Thanks for your time