如何使用CTE重写查询

时间:2009-11-19 07:43:09

标签: sql sql-server

我有这样的查询

DECLARE @A INT
SET @A = 22

SELECT  Moo, 1st - 2nd + 100 AS WWW
FROM    (
    SELECT  1 + Num AS Moo,
        ((@A-100)*3)+num AS 1st,
        ((@A-100)*4)+num AS  2nd
    FROM    tblC
    WHERE   ColA = 'Atic' AND Num < 7
    ) AS TTT

如何使用CTE重写相同的查询?

1 个答案:

答案 0 :(得分:1)

WITH TTT AS (
 SELECT 1+Num as Moo,
   ((@A-100)*3)+num as [1st],
   ((@A-100)*4)+num as [2nd]
 FROM tblC
 WHERE ColA = 'Atic' and Num < 7)
SELECT Moo, [1st]-[2nd]+100 AS WWW
FROM TTT;