我有以下代码,它将结果显示在包含多个列的表中。
SELECT
ref, design,
ISNULL((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 34 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'LPP',
ISNULL((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 28 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'LPM',
ISNULL((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 42 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'RNPM',
ISNULL((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 68 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'RAM',
ISNULL((SELECT Sum(qtt2) FROM bi(nolock) WHERE bi.ndos = 68 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'RA' ,
ISNULL((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 59 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'GD'
FROM
bi(nolock) temp
WHERE
(temp.ndos = 34 OR temp.ndos = 28 OR temp.ndos = 42 OR temp.ndos = 68)
AND temp.bofref LIKE #1#
AND temp.ref NOT LIKE ' '
GROUP BY
ref, design
ORDER BY
ref
我现在想要在新列中对某些select的值求和,例如我想对列进行求和" LPP,RNPM和RA"在整个专栏中,但我无法让它发挥作用。
怎么做?
答案 0 :(得分:0)
最直接的方法是将您的查询包装在另一个中:
SELECT sub.*,
LPP + RNPM + RA as LPP_RNPM_RA
FROM (
SELECT ref, design,
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 34 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'LPP',
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 28 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'LPM',
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 42 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'RNPM',
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 68 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'RAM',
Isnull((SELECT Sum(qtt2) FROM bi(nolock) WHERE bi.ndos = 68 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'RA' ,
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 59 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'GD'
FROM bi(nolock) temp
WHERE temp.ndos IN (34, 28, 42, 68)
AND temp.bofref like #1#
AND temp.ref NOT LIKE ' '
GROUP BY ref,
design
) as sub
ORDER BY ref
请注意,您可以使用IN
运算符代替OR
子句中的where
运算符。
此外,你总结的方式看起来很尴尬:不要做子查询,但是在你的select
中执行此操作:
Sum(CASE WHEN temp.ndos = 34 THEN qtt ELSE 0 END) AS 'LPP',
......等等。
答案 1 :(得分:0)
如果我理解正确,你可以总结单列
SELECT
ref
, design
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 34 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'LPP',
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 28 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'LPM',
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 42 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'RNPM',
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 68 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'RAM',
Isnull((SELECT Sum(qtt2) FROM bi(nolock) WHERE bi.ndos = 68 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'RA' ,
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 59 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) AS 'GD',
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 34 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) +
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 28 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) +
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 42 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) +
Isnull((SELECT Sum(qtt) FROM bi(nolock) WHERE bi.ndos = 68 AND bofref like #1# AND bi.ref = temp.ref GROUP BY ref), 0) as TOTAL_COLUMN
FROM bi(nolock) temp
WHERE ( temp.ndos = 34
OR temp.ndos = 28
OR temp.ndos = 42
OR temp.ndos = 68 )
AND temp.bofref like #1#
AND temp.ref NOT LIKE ' '
GROUP BY ref,
design
ORDER BY ref