WITH changed AS (
    SELECT "DHINV#" FROM lgdat.oih WHERE DHIDAT BETWEEN '{oid_wm}' AND CURRENT_DATE
    UNION SELECT "DHINV#" FROM lgdat.oih WHERE DHPDAT BETWEEN '{oid_wm}' AND CURRENT_DATE
    UNION SELECT "DHINV#" FROM lgdat.oih WHERE DHPOST <> 'Y'
)
SELECT
  "DIINV#" AS "diinv#",
  "DILIN#" AS "dilin#",
  "DIORD#" AS "diord#",
  "DIITM#" AS "diitm#",
  DIQTBO AS diqtbo,
  DIQTSH AS diqtsh,
  RTRIM(DIUNIT) AS diunit,
  DIPRIC AS dipric,
  RTRIM(DIPRUN) AS diprun,
  DIRETP AS diretp,
  RTRIM(DIRETU) AS diretu,
  DIEXT AS diext,
  RTRIM(DIPSTX) AS dipstx,
  RTRIM(DIFTXC) AS diftxc,
  RTRIM(DITXGR) AS ditxgr,
  RTRIM(DITXRT) AS ditxrt,
  RTRIM(DIDESC) AS didesc,
  RTRIM(DIMAJR) AS dimajr,
  RTRIM(DIMINR) AS diminr,
  RTRIM(DIPART) AS dipart,
  RTRIM(DIPREP) AS diprep,
  RTRIM(DIRELN) AS direln,
  RTRIM(DIGLCD) AS diglcd,
  RTRIM(DIREAS) AS direas,
  RTRIM(DISLMN) AS dislmn,
  DICPCT AS dicpct,
  DICRAT AS dicrat,
  DICOST AS dicost,
  DICTEX AS dictex,
  DIQTSO AS diqtso,
  RTRIM(DIORUN) AS diorun,
  DIQTSP AS diqtsp,
  DIEXTN AS diextn,
  DITDIS AS ditdis,
  DIPSTA AS dipsta,
  DIFSTA AS difsta,
  DITAXA AS ditaxa,
  DIFTAM AS diftam,
  RTRIM(DISTKL) AS distkl,
  DILBCT AS dilbct,
  DIBRCT AS dibrct,
  DIMLCT AS dimlct,
  DIOTCT AS diotct,
  RTRIM(DIRAN) AS diran,
  DIOLST AS diolst,
  RTRIM(DIFUT1) AS difut1,
  RTRIM(DIFUT2) AS difut2,
  RTRIM(DIFUT3) AS difut3,
  RTRIM(DIFUT4) AS difut4,
  RTRIM(DIFUT5) AS difut5,
  RTRIM(DIFUT6) AS difut6,
  DIBRFC AS dibrfc,
  DIBRVC AS dibrvc,
  DIFUT7 AS difut7,
  DIFUT8 AS difut8,
  RTRIM(DIBLHL) AS diblhl,
  DIIRNK AS diirnk,
  DISTPR AS distpr,
  RTRIM(DIPJCD) AS dipjcd,
  DIWCN AS diwcn,
  DIWCDN AS diwcdn,
  RTRIM(DIADAL) AS diadal,
  DIACRN AS diacrn,
  DIACRL AS diacrl,
  RTRIM(DIVSXF) AS divsxf,
  RTRIM(DISRVC) AS disrvc,
  DIEXCR AS diexcr,
  CASE WHEN DIFDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DIFDAT END AS difdat,
  CASE WHEN DITDAT IN (DATE('0001-01-01'), DATE('9999-12-31')) THEN NULL ELSE DITDAT END AS ditdat,
  DIRBIV AS dirbiv,
  DIRBIL AS dirbil,
  RTRIM(DISONM) AS disonm,
  DISOLN AS disoln,
  RTRIM(DIMMEL) AS dimmel,
  DIOITM AS dioitm,
  RTRIM(DIFUT9) AS difut9,
  RTRIM(DIFUT10) AS difut10,
  RTRIM(DIFUT11) AS difut11,
  RTRIM(DIFUT12) AS difut12,
  RTRIM(DIFUT13) AS difut13,
  RTRIM(DIFUT14) AS difut14,
  RTRIM(DIFUT15) AS difut15,
  RTRIM(DIFUT16) AS difut16,
  RTRIM(DIFUT17) AS difut17,
  DIFUT18 AS difut18,
  DIFUT19 AS difut19,
  DIFUT20 AS difut20
FROM LGDAT.OID
WHERE "DIINV#" IN (SELECT "DHINV#" FROM changed)
