WITH changed AS (
    SELECT dcord# FROM lgdat.qcrh WHERE dcudat BETWEEN '{qcri_wm}' AND CURRENT_DATE
    UNION
    SELECT dcord# FROM lgdat.qcrh WHERE dcodat BETWEEN '{qcri_wm}' AND CURRENT_DATE
    UNION
    SELECT dcord# FROM lgdat.qcrh WHERE dccdat BETWEEN '{qcri_wm}' AND CURRENT_DATE
)
SELECT
  "DDORD#" AS "ddord#",
  "DDITM#" AS "dditm#",
  "DDDES#" AS "dddes#",
  DDQTOI AS ddqtoi,
  DDQTBI AS ddqtbi,
  DDQTSI AS ddqtsi,
  RTRIM(DDUNIT) AS ddunit,
  DDUNPR AS ddunpr,
  DDTOTI AS ddtoti,
  RTRIM(DDSLMN) AS ddslmn,
  DDCPCT AS ddcpct,
  DDCRAT AS ddcrat,
  RTRIM(DDAIND) AS ddaind,
  RTRIM(DDPTEX) AS ddptex,
  RTRIM(DDFTCD) AS ddftcd,
  CASE WHEN DDRDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DDRDAT END AS ddrdat,
  RTRIM(DDMAJS) AS ddmajs,
  RTRIM(DDMINS) AS ddmins,
  RTRIM(DDITST) AS dditst,
  RTRIM(DDPART) AS ddpart,
  RTRIM(DDGLC) AS ddglc,
  RTRIM(DDCRRS) AS ddcrrs,
  CASE WHEN DDSDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DDSDAT END AS ddsdat,
  RTRIM(DDTXGR) AS ddtxgr,
  RTRIM(DDTXRT) AS ddtxrt,
  RTRIM(DDNU07) AS ddnu07,
  RTRIM(DDTMPS) AS ddtmps,
  DDTEMP AS ddtemp,
  DDQTOO AS ddqtoo,
  DDQTBO AS ddqtbo,
  DDQTSO AS ddqtso,
  RTRIM(DDORUN) AS ddorun,
  DDQTSP AS ddqtsp,
  RTRIM(DDPRUN) AS ddprun,
  DDTOTB AS ddtotb,
  DDTDIS AS ddtdis,
  DDTTAX AS ddttax,
  DDTFRT AS ddtfrt,
  DDTWGT AS ddtwgt,
  RTRIM(DDTWUN) AS ddtwun,
  DDTVOL AS ddtvol,
  RTRIM(DDTVUN) AS ddtvun,
  CASE WHEN DDODAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DDODAT END AS ddodat,
  RTRIM(DDSTKL) AS ddstkl,
  RTRIM(DDSTOR) AS ddstor,
  CASE WHEN DDDDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DDDDAT END AS ddddat,
  CASE WHEN DDFDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DDFDAT END AS ddfdat,
  CASE WHEN DDHDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DDHDAT END AS ddhdat,
  CASE WHEN DDQDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DDQDAT END AS ddqdat,
  CASE WHEN DDCDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DDCDAT END AS ddcdat,
  DDOLST AS ddolst,
  DDCTMS AS ddctms,
  RTRIM(DDCUSR) AS ddcusr,
  RTRIM(DDDBUY) AS dddbuy,
  "DDREQ#" AS "ddreq#",
  RTRIM(DDPJNM) AS ddpjnm,
  RTRIM(DDFUT1) AS ddfut1,
  RTRIM(DDFUT2) AS ddfut2,
  RTRIM(DDFUT3) AS ddfut3,
  RTRIM(DDFUT4) AS ddfut4,
  RTRIM(DDFUT5) AS ddfut5,
  RTRIM(DDFUT6) AS ddfut6,
  DDFUT7 AS ddfut7,
  DDFUT8 AS ddfut8,
  RTRIM(DDFUT9) AS ddfut9,
  RTRIM(DDFUTA) AS ddfuta,
  RTRIM(DDFUTB) AS ddfutb,
  RTRIM(DDFUTC) AS ddfutc,
  RTRIM(DDFUTD) AS ddfutd,
  DDWTCH AS ddwtch,
  DDWTCI AS ddwtci,
  DDLDSQ AS ddldsq,
  RTRIM("DDRAN#") AS "ddran#",
  RTRIM(DDCTMZ) AS ddctmz,
  RTRIM(DDOTMZ) AS ddotmz,
  RTRIM(DDRTMZ) AS ddrtmz,
  RTRIM(DDQTMZ) AS ddqtmz,
  RTRIM(DDSTMZ) AS ddstmz,
  DDRTME AS ddrtme,
  DDQTME AS ddqtme,
  DDSTME AS ddstme,
  CASE WHEN DDRXDT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DDRXDT END AS ddrxdt,
  DDRXTM AS ddrxtm,
  CASE WHEN DDQXDT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DDQXDT END AS ddqxdt,
  DDQXTM AS ddqxtm,
  RTRIM(DDPTYP) AS ddptyp,
  RTRIM(DDAWLS) AS ddawls,
  DDPOIT AS ddpoit,
  RTRIM(DDSRVC) AS ddsrvc,
  RTRIM(DDPRCL) AS ddprcl,
  RTRIM(DDAVAC) AS ddavac,
  RTRIM(DDDBAC) AS dddbac,
  RTRIM(DDSCDP) AS ddscdp,
  RTRIM(DDSOCP) AS ddsocp,
  RTRIM(DDCSPI) AS ddcspi,
  RTRIM(DDSCPI) AS ddscpi,
  RTRIM(DDSCOR) AS ddscor,
  RTRIM(DDSCIM) AS ddscim,
  RTRIM("DDREV#") AS "ddrev#",
  RTRIM(DDCSPA) AS ddcspa,
  DDOITM AS ddoitm,
  RTRIM(DDFROM) AS ddfrom,
  RTRIM(DDMMEL) AS ddmmel,
  RTRIM(DDFEAT) AS ddfeat,
  RTRIM(DDOPTN) AS ddoptn
FROM LGDAT.QCRI i
INNER JOIN changed c ON c.dcord# = i.ddord#
