我有3個表Tablle1,Table2,Table3。 表1 &表2與Id1相關。 表2 &表3與Id2相關。SQL計算子查詢
我有以下查詢哪些工作。我如何獲得CTotal = C00 + C01 + C02?
我在哪裏添加OUTPUT語句來返回STOTAL的值& CTOTAL?
SELECT X.Id1, X.S00, X.S01, X.S02, X.S00 + X.S01 + X.S02 AS STOTAL,
CEILING(X.S00/@Size) AS C00, CEILING(X.S01/@Size) AS C01,
CEILING(X.S02/@Size) AS C02,
Tble1.Select00, Table1.Select01, Table1.Select02
FROM(
SELECT Table1.Id1,
SUM(CASE WHEN Table1.Select00 = 1 THEN Table3.Num00 ELSE 0 END) AS S00,
SUM(CASE WHEN Table1.Select01 = 1 THEN Table3.Num01 ELSE 0 END) AS S01,
SUM(CASE WHEN Table1.Select02 = 1 THEN Table3.Num02 ELSE 0 END) AS S02
FROM Table1 INNER JOIN
Table2 ON Table1.Id1 = Table2.Id1 INNER JOIN
Table3 ON Table2.Id2 = Table3.Id2
WHERE (Table1.Id1 = @Id1
)
GROUP BY Table1.Id1) AS X INNER JOIN Table1 ON X.Id1 = Table1.Id1
凡在您的查詢的C03?我沒看到它。 – Icarus