我的查询中有错误ORA-01722。 这段代码获取了命令列表。我需要添加一个子查询来获取最后排序的命令。以下代码运行良好(使用业务对象生成),我尝试添加我留在评论中的行
SELECT
ltrim(SAP_MARA.MATNR,'0'),
SAP_MAKT.MAKTX,
SAP_MARA.MTART,
SAP_MARA.MATKL,
SAP_EKKO.EKGRP,
SAP_EKPA.LIFN2,
SAP_EKKO.EBELN,
SAP_EKPO.netwr,
SAP_EKKO.AEDAT
-- r.lastDate
FROM
SAP_MARA,
SAP_MAKT,
SAP_EKKO,
SAP_EKPA,
SAP_EKPO
--,( select EBELN , max(AEDAT) as lastDate from SAP_EKKO group by EBELN) r
WHERE
( SAP_EKKO.MANDT=SAP_EKPO.MANDT and SAP_EKKO.EBELN=SAP_EKPO.EBELN )
AND ( SAP_MAKT.MATNR(+)=SAP_MARA.MATNR
and SAP_MAKT.SPRAS(+)='F' )
AND ( SAP_MARA.MATNR(+)=SAP_EKPO.MATNR )
AND ( SAP_EKPO.EBELN like '45%'
and SAP_EKPO.MATNR<>' ' )
AND ( SAP_EKKO.EBELN like '45%' and SAP_EKKO.LIFNR<>' ' )
AND ( SAP_EKPA.MANDT=SAP_EKKO.MANDT and SAP_EKPA.EBELN=SAP_EKKO.EBELN )
AND ( SAP_EKKO.BUKRS= '1000' )
--and SAP_EKKO.AEDAT = r.lastDate
;
当我将其作为查询调用时,子查询也可以正常工作。 我知道这个错误来自尝试将字符串转换为数字(可能在WHERE子句中),但我不知道为什么只有当我添加这几行时才会发生这种情况。
有人可以帮助我吗?
答案 0 :(得分:1)
我相信如果您移动自联接,您应该能够避免错误。请尝试以下方法:
SELECT
ltrim(SAP_MARA.MATNR,'0'),
SAP_MAKT.MAKTX,
SAP_MARA.MTART,
SAP_MARA.MATKL,
k.EKGRP,
SAP_EKPA.LIFN2,
k.EBELN,
SAP_EKPO.netwr,
k.AEDAT
FROM
SAP_MARA,
SAP_MAKT,
SAP_EKKO k,
SAP_EKPA,
SAP_EKPO
WHERE
( SAP_EKKO.MANDT=SAP_EKPO.MANDT and SAP_EKKO.EBELN=SAP_EKPO.EBELN )
AND ( SAP_MAKT.MATNR(+)=SAP_MARA.MATNR
and SAP_MAKT.SPRAS(+)='F' )
AND ( SAP_MARA.MATNR(+)=SAP_EKPO.MATNR )
AND ( SAP_EKPO.EBELN like '45%'
and SAP_EKPO.MATNR<>' ' )
AND ( SAP_EKKO.EBELN like '45%' and SAP_EKKO.LIFNR<>' ' )
AND ( SAP_EKPA.MANDT=SAP_EKKO.MANDT and SAP_EKPA.EBELN=SAP_EKKO.EBELN )
AND ( SAP_EKKO.BUKRS= '1000' )
and k.AEDAT = (select max(AEDAT) from SAP_EKKO k2 where k2.ebeln = k.ebeln)
;
答案 1 :(得分:0)
我确实简化了代码。命令编号格式为NUMBER。
希望它有所帮助。
Select
Command_Number,
LastDateOrder
from
SAP_EKKO,
( select Command_Number , max(DATE) as lastDate from SAP_EKKO group by
Command_Number) r
where
SAP_EKKO.Command_Number = r.Command_Number