我有一个查询,该查询返回运动的总数:
SELECT
CASE PROD_TYPE
WHEN 1 THEN SUM(WEIGHT)
WHEN 2 THEN SUM(CASE MOVIMENT
1 THEN SUM(QUANTITY)
2 THEN SUM(QUANTITY * -1)
END)
END AS TOTAL
FROM TABLE
GROUP BY PROD_TYPE, MOVIMENT
此查询给我错误ORA-00937,如果删除最后一个总和不给出错误,但结果不正确:
WHEN 2 THEN CASE MOVIMENT
1 THEN SUM(QUANTITY)
2 THEN SUM(QUANTITY * -1)
END
edit1:
我有以下数据
PROD_TYPE |数量|重量运动
-------- 2 ------------ 1100 ------------- 0 ----------- --1 ---
-------- 2 ------------ 1000 ------------- 0 ------------- 1 ---
-------- 2 ------------ 1200 ------------- 0 ------------- 1 ---
-------- 2 ------------ 1200 ------------- 0 ------------- 2 ---
* 1输入,2输出
正确的结果是2100,但是如果我删除最后一个总和,它将给出两个结果:3300和-1200
我试图将QUANTITY放在group by子句中,但始终出现相同的错误。
答案 0 :(得分:1)
我想你想要
SELECT (CASE PROD_TYPE
WHEN 1 THEN SUM(WEIGHT)
WHEN 2 THEN SUM(CASE MOVIMENT
WHEN 1 THEN QUANTITY
WHEN 2 THEN - QUANTITY
END)
END) AS TOTAL
FROM TABLE
GROUP BY PROD_TYPE, MOVIMENT;