我正在通过ODBC和Crystal Report 2008使用oracle 11(不确定确切的版本,但是由于LISTAGG无法正常工作,我想它不是第2版)。
这是我遇到的问题:
这是一张桌子:
TABLE ODB.TASK_CARD_CONTROL ------------------------------------------ task_card control_category code ------------------------------------------ 1 zone 17 1 zone 33 1 zone 21 2 zone 18 2 zone 05 3 zone 55 3 zone 32 3 zone 72
我正在使用WM_CONCAT函数来获取如下内容:
task_card zones 1 17,33,21 2 18,05 3 55,32,72
这是用于此的SQL:
SELECT TASK_CARD, WM_CONCAT(code) as ZONES FROM ODB.TASK_CARD_CONTROL WHERE ODB.TASK_CARD_CONTROL.CONTROL_CATEGORY = 'ZONE' GROUP BY TASK_CARD
但我希望对区域进行排序,因此我尝试了以下操作:
SELECT TASK_CARD, WM_CONCAT(code) as ZONES FROM (SELECT TASK_CARD, CODE, CONTROL_CATEGORY FROM ODB.TASK_CARD_CONTROL ORDER BY CODE) WHERE ODB.TASK_CARD_CONTROL.CONTROL_CATEGORY = 'ZONE' GROUP BY TASK_CARD
但由于某种原因,它返回以下错误:
Failed to retrieve data from the database. Details: 42S22:[Oracle][ODBC][Ora]ORA-00904: "ODB"."TASK_CARD_CONTROL"."CONTROL_CATEGORY" : invalid identifier
我真的不明白我在这里做错了什么…有人可以给我一个提示吗?
您不能从内部查询外部引用ODB.TASK_CARD_CONTROL.CONTROL_CATEGORY。尝试:
SELECT TASK_CARD, WM_CONCAT(code) as ZONES FROM (SELECT TASK_CARD, CODE, CONTROL_CATEGORY FROM ODB.TASK_CARD_CONTROL WHERE ODB.TASK_CARD_CONTROL.CONTROL_CATEGORY = 'ZONE' ORDER BY CODE) GROUP BY TASK_CARD