I have a query that has something like this:
;with Dataset1 as (SELECT columnA, columnB, SUM(columnC) as columnC FROM Table1 GROUP BY columnA, columnB )
SELECT
(SELECT columnC FROM Dataset1 WHERE columnA = '1' and columnB='2') as Result1,
(SELECT columnC FROM Dataset1 WHERE columnA = '2' and columnB='3') as Result2,
(SELECT columnC FROM Dataset1 WHERE columnA = '3' and columnB='4') as Result3,
...
What I'm wondering is does Dataset1 get selected in-memory and reused with every one of those select clauses, or does SQL fetch it each time? If that's the case, that seems really wasteful, and what should I do to optimize this ?