如何纠正此错误

时间:2016-04-05 08:02:49

标签: sql oracle

SELECT CU.CUST_CODE 
FROM AR_CUSTOMERS CU 
WHERE CU.CUST_CODE = TM.CUST_CODE
UNION
SELECT ITS.ITEMS_CODE
FROM AR_ITEMS ITS
WHERE ITS.ITEMS_CODE = TM.ITEM_CODE
UNION
SELECT ITS.ITEMS_NAME
FROM AR_ITEMS ITS
WHERE TM.INVOICE_NO = TD.INVOICE_NO AND ITS.ITEMS_CODE = TD.ITEM_CODE
UNION
SELECT TO_CHAR(TM.JOB_DATE,'MON-YY')MON FROM AR_TRANS_MASTER TM
UNION
SELECT TO_CHAR(TM.JOB_DATE,'MM-YY')MM  FROM AR_TRANS_MASTER TM
UNION
SELECT SUM(TD.QUANTITY)QUANTITY, SUM(TD.PRICE)UNIT
FROM AR_TRANS_DETAIL TD
WHERE ITS.ITEMS_CODE = TD.ITEM_CODE
AND 
TM.JOB_DATE BETWEEN :STARTDATE AND :STOPDATE
GROUP BY 
CU.CUST_CODE,ITS.ITEMS_CODE,ITS.ITEMS_NAME,TO_CHAR(TM.JOB_DATE,'MON-YY'),TO_CHAR(TM.JOB_DATE,'MM-YY')
ORDER BY
MM ASC;
  

ORA-00904:“TM”。“CUST_CODE”:标识符无效   00904. 00000 - “%s:无效标识符”   *原因:
  *动作:行错误:3列:22

1 个答案:

答案 0 :(得分:2)

您将多个独立查询中的各个选择列表合并在一起,这些查询无法看到彼此的数据或结果;哪些是不同数量的列和不同的数据类型。有几个没有限制,可能会得到很多你不期望的数据。

您应该一起加入表格,而不是联合单独的查询;类似的东西:

SELECT CU.CUST_CODE, ITS.ITEMS_CODE, ITS.ITEMS_NAME, TO_CHAR(TM.JOB_DATE,'MON-YY') MON,
  TO_CHAR(TM.JOB_DATE,'MM-YY') MM, SUM(TD.QUANTITY) QUANTITY, SUM(TD.PRICE) UNIT
FROM AR_TRANS_MASTER TM
JOIN AR_CUSTOMERS CU 
ON CU.CUST_CODE = TM.CUST_CODE
JOIN AR_ITEMS ITS
ON ITS.ITEMS_CODE = TM.ITEM_CODE
JOIN AR_TRANS_DETAIL TD
ON TD.INVOICE_NO = TM.INVOICE_NO
AND TD.ITEM_CODE = ITS.ITEMS_CODE
WHERE TM.JOB_DATE BETWEEN :STARTDATE AND :STOPDATE
GROUP BY CU.CUST_CODE, ITS.ITEMS_CODE, ITS.ITEMS_NAME, TO_CHAR(TM.JOB_DATE,'MON-YY'),
  TO_CHAR(TM.JOB_DATE,'MM-YY')
ORDER BY MM ASC;

未经测试,您需要检查连接条件是否已按预期转换。

您可以在文档中详细了解the UNION set operatorjoins