PostgreSQL의 9.5CTE에서 JOIN 작업을 주문할 수 있습니까? CTE 아래 (PostgreSQL을)
는 DOCTOR_PROCEDURES에 테이블 CPT에서 기록과 CPT_INVOICE를 이동하고 DOCTORBILLING 그에 따라 UID 업데이트 할 올바르게 작동합니다. 그러나 CPT_INVOICE는 상위 CPT에 대한 외래 키를 가지므로이 외래 키 관계가 제거 될 때까지이 스크립트는 실패합니다.
PostgreSQL에서 CTE를 특정 순서로 실행하도록하는 방법이 있습니까? 즉, 먼저 planB보다 먼저 planC를 실행 하시겠습니까? 당신이 무거운 순서 종속성을 당겨 경우
WITH planA AS (
select cpt_recid from doctorbilling
),
planC as (
delete from cpt_invoice D
USING planA a
where D.recid = A.cpt_recid
returning D.cpt_recid, D.ninsurance, D.ncash, D.mustschedule, D.doneinoffice
),
planB as (
delete from cpt C
USING planA A
where C.recid = A.cpt_recid
returning C.recid as cpt_recid, C.code, C.cdesc, C.procedure_type, C.sex
),
planD as (
insert into doctor_procedures (code, cdesc, procedure_type, sex, ninsurance, ncash, mustschedule, doneinoffice, cpt_recid)
select distinct on (b.cdesc) b.code, b.cdesc, b.procedure_type, b.sex, c.ninsurance, c.ncash, c.mustschedule, c.doneinoffice, a.cpt_recid
from planA A
join planB B on B.cpt_recid = A.cpt_recid
left join planC C on C.cpt_recid = A.cpt_recid -- there may not be a cpt_invoice for the cpt_recid.
order by b.cdesc
returning cpt_recid, uid
)
update doctorbilling T
set uid = D.uid
from planD D
where T.cpt_recid = D.cpt_recid
좋은 물건! 나는 plpgsql을 사용하지 않을 것이라고 생각했다. 감사. –
그냥 참고로. 위의 CTE를 사용하여 하나의 레코드 만 마이그레이션하는 것이 아니라 전체 테이블을 마이그레이션합니다. –