我想将oracle查询迁移到postgresql:
这是甲骨文版本:
SELECT
replace(replace(replace(
cf.NO_FACTURE||';'||
cf.MODULE||';'||
replace(replace(replace(cf.REF_FACTURE_SYBEL,'&',' '),'''',''''''),';','')||';'||
nvl(replace(to_char(cf.SOLDE_VERSE),',','.'),0)||';'||
cf.DATE_PAIEMENT_FACTURE||';'||
nvl(replace(to_char(cf.MONTANT_ACOMPTE_VERSE),',','.'),0)
, CHR(10),''),CHR(13),''),'*',' ')
FROM
(SELECT distinct td_inf.NO_FACTURE
FROM tac_dossier td_inf,
com_agent ca,
com_habilitation ch
WHERE td_inf.etat_dossier = 'id.15.ClosFacturation'
AND MONTHS_BETWEEN(sysdate,td_inf.date_chgt_etat) > $nb_mois_clos
AND td_inf.cod_ag_resp = ca.cod_ag(+)
AND ch.cod_ag = ca.cod_ag
AND ch.cod_ent in (select cod_ent from com_ent_ft where pole = td_inf.cod_ent or cod_ent = td_inf.cod_ent)) td,
COM_FACTURE cf
WHERE cf.NO_FACTURE = td.NO_FACTURE;下面是查询的postgresql版本:
SELECT
replace(replace(replace(
cf.NO_FACTURE||';'||
cf.MODULE||';'||
replace(replace(replace(cf.REF_FACTURE_SYBEL,'&',' '),'''',''''''),';','')||';'||
COALESCE(replace(to_char(cf.SOLDE_VERSE),',','.'),'0')||';'||
cf.DATE_PAIEMENT_FACTURE||';'||
COALESCE(replace(to_char(cf.MONTANT_ACOMPTE_VERSE),',','.'),'0')
, CHR(10),''),CHR(13),''),'*',' ')
FROM
(SELECT distinct td_inf.NO_FACTURE
FROM
tac_dossier td_inf
LEFT OUTER JOIN com_agent ca ON td_inf.cod_ag_resp = ca.cod_ag
INNER JOIN com_ent_ft cef ON (cef.pole = td_inf.cod_ent or cef.cod_ent = td_inf.cod_ent)
INNER JOIN com_habilitation ch ON (ch.cod_ag = ca.cod_ag and ch.cod_ent = cef.cod_ent)
WHERE td_inf.etat_dossier = 'id.15.ClosFacturation'
AND EXTRACT(month from age(NOW(),td_inf.date_chgt_etat)) > 2
) td,
COM_FACTURE cf
WHERE cf.NO_FACTURE = td.NO_FACTURE;查询正在工作,语法上没有问题,但这两个查询的结果是不同的。
这上面有便条吗?
谢谢。
发布于 2020-04-16 19:36:59
这是:
AND EXTRACT(month from age(NOW(),td_inf.date_chgt_etat)) > 2 与最初的Oracle条件不同。
提取函数不以月份为单位获得与间隔相等的值,而只是获得“月份”字段。因此,如果间隔为“1年0个月2天16小时”,extract(month from ...)将返回0。
你想要的是:
and age(now(), td_inf.date_chgt_etat) > interval '2 months'或者,如果您想要在列上使用索引,请使用:
and td_inf.date_chgt_etat < now() - interval '2 months'您的查询也缺少
AND ch.cod_ent in (...) 您已经将其转换为一个JOIN,它做了一些不同的事情。这是否改变了结果取决于两个表com_ent_ft和com_habilitation是如何关联的。
https://dba.stackexchange.com/questions/265196
复制相似问题