我正在努力为我们在损坏自己设备的地方完成维修工作(我知道......这很疯狂)。
基本上,我希望能够查看零件的总维修时数,然后还可以查看通过不同类型的维修分解了多少小时。我的代码如下,我希望实现这一目标:
我的代码如下:
SELECT
PART_NUM,
PART_NAME,
PART_SERIAL_NUM,
SUM(REPAIR_HOURS) AS "TOTAL_REPAIR_HOURS",
CASE WHEN REPAIR_TYPE = 'A' THEN SUM(REPAIR_HOURS) END AS "CONCRETE_HOURS",
CASE WHEN REPAIR_TYPE = 'B' THEN SUM(REPAIR_HOURS) END AS "DEMOLITION_HOURS",
CASE WHEN REPAIR_TYPE = 'C' THEN SUM(REPAIR_HOURS) END AS "AUTOMOTIVE_HOURS"
FROM
PARTS
LEFT JOIN REPAIRS
ON PART_NUM = REPAIRED_PART AND
PART_SERIAL_NUM = REPAIRED_PART_SERIAL_NUM
WHERE
PART_NUM = '500ABX'
GROUP BY
PART_NUM,
PART_NAME,
PART_SERIAL_NUM
(我显然一次只在一个部件上进行测试:))
我不断收到错误
ORA-00979:不是GROUP BY表达式(#979)。
我试图删除CASE WHEN语句,它运行正常。只有在我添加这些内容时,我才会遇到问题。
任何帮助将不胜感激! :)
答案 0 :(得分:4)
你需要案例表达式里面聚合,而不是反过来:
SELECT
PART_NUM,
PART_NAME,
PART_SERIAL_NUM,
SUM(REPAIR_HOURS) AS "TOTAL_REPAIR_HOURS",
SUM(CASE WHEN REPAIR_TYPE = 'A' THEN REPAIR_HOURS ELSE 0 END) AS "CONCRETE_HOURS",
SUM(CASE WHEN REPAIR_TYPE = 'B' THEN REPAIR_HOURS ELSE 0 END) AS "DEMOLITION_HOURS",
SUM(CASE WHEN REPAIR_TYPE = 'C' THEN REPAIR_HOURS ELSE 0 END) AS "AUTOMOTIVE_HOURS"
FROM
PARTS
LEFT JOIN REPAIRS
ON PART_NUM = REPAIRED_PART AND
PART_SERIAL_NUM = REPAIRED_PART_SERIAL_NUM
WHERE
PART_NUM = '500ABX'
GROUP BY
PART_NUM,
PART_NAME,
PART_SERIAL_NUM