SQL"重用" WITH子句的结果?

时间:2018-03-15 15:44:13

标签: sql-server-2008 common-table-expression

我有一个类似这样的查询:

;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,
...

我想知道的是,Dataset1是否在内存中被选中并重复使用每个select子句,或者SQL每次都会获取它?如果是这样的话,这看起来真的很浪费,我该怎么做才能优化呢?

0 个答案:

没有答案