WITH changed AS (
    SELECT "FEBOL#" FROM lgdat.bolh
    WHERE (FECDAT BETWEEN '{bold_wm}' AND CURRENT_DATE)
       OR (FEPDAT BETWEEN '{bold_wm}' AND CURRENT_DATE)
       OR (FEDDAT BETWEEN '{bold_wm}' AND CURRENT_DATE)
       OR (FEPDAT IS NULL AND FEDDAT IS NULL AND FECDAT >= CURRENT_DATE - 90 DAYS)
)
SELECT
  "FGBOL#" AS "fgbol#",
  "FGENT#" AS "fgent#",
  "FGORD#" AS "fgord#",
  FGITEM AS fgitem,
  "FGPO#" AS "fgpo#",
  FGPREL AS fgprel,
  FGPITM AS fgpitm,
  FGQSHP AS fgqshp,
  FGQSHO AS fgqsho,
  RTRIM(FGCTNC) AS fgctnc,
  FGCTNN AS fgctnn,
  FGPLTN AS fgpltn,
  FGNTWC AS fgntwc,
  FGGRSC AS fggrsc,
  FGTARC AS fgtarc,
  FGVOLC AS fgvolc,
  FGQTYC AS fgqtyc,
  FGWGTP AS fgwgtp,
  FGVOLP AS fgvolp,
  RTRIM(FGLSTS) AS fglsts,
  RTRIM(FGPART) AS fgpart,
  RTRIM(FGRLNO) AS fgrlno,
  "FGINV#" AS "fginv#",
  "FGLIN#" AS "fglin#",
  RTRIM(FGORUN) AS fgorun,
  FGFTAM AS fgftam,
  RTRIM(FGSTKL) AS fgstkl,
  RTRIM("FGRAN#") AS "fgran#",
  RTRIM("FGCPT#") AS "fgcpt#",
  RTRIM(FGCNID) AS fgcnid,
  RTRIM(FGECHG) AS fgechg,
  FGCCUM AS fgccum,
  FGPCUM AS fgpcum,
  RTRIM("FGCPO#") AS "fgcpo#",
  RTRIM(FGCMPR) AS fgcmpr,
  RTRIM(FGDOCK) AS fgdock,
  RTRIM(FGISTS) AS fgists,
  RTRIM(FGSREF) AS fgsref,
  RTRIM(FGCRCM) AS fgcrcm,
  RTRIM(FGUSR1) AS fgusr1,
  RTRIM(FGUSR2) AS fgusr2,
  RTRIM(FGUSR3) AS fgusr3,
  "FGREQ#" AS "fgreq#",
  FGDREQ AS fgdreq,
  FGJITD AS fgjitd,
  RTRIM(FGUSR4) AS fgusr4,
  RTRIM(FGUSR5) AS fgusr5,
  RTRIM(FGFUT1) AS fgfut1,
  RTRIM(FGFUT2) AS fgfut2,
  RTRIM(FGFUT3) AS fgfut3,
  RTRIM(FGFUT4) AS fgfut4,
  RTRIM(FGFUT5) AS fgfut5,
  FGFUTA AS fgfuta,
  FGFUTB AS fgfutb,
  FGFUTC AS fgfutc,
  FGFUTD AS fgfutd,
  RTRIM(FGFUTE) AS fgfute,
  RTRIM(FGFUTF) AS fgfutf,
  RTRIM(FGFUTG) AS fgfutg,
  RTRIM(FGFUTH) AS fgfuth,
  RTRIM(FGFUTI) AS fgfuti,
  RTRIM(FGFUTJ) AS fgfutj,
  RTRIM(FGFUTK) AS fgfutk,
  RTRIM(FGFUTL) AS fgfutl,
  RTRIM(FGFUTM) AS fgfutm,
  RTRIM(FGFUTN) AS fgfutn,
  RTRIM(FGFUTO) AS fgfuto,
  RTRIM(FGFUTP) AS fgfutp,
  RTRIM(FGFUTQ) AS fgfutq,
  RTRIM(FGFUTR) AS fgfutr,
  RTRIM(FGFUTS) AS fgfuts,
  RTRIM(FGFUTT) AS fgfutt,
  RTRIM(FGPLNT) AS fgplnt,
  RTRIM(FGBUNT) AS fgbunt,
  RTRIM(FGRPBR) AS fgrpbr,
  FGNWFP AS fgnwfp,
  RTRIM(FGCREL) AS fgcrel,
  RTRIM(FGPSLP) AS fgpslp,
  RTRIM(FGRMID) AS fgrmid,
  RTRIM(FGCREV) AS fgcrev,
  RTRIM(FGREFF) AS fgreff,
  RTRIM(FGFUT6) AS fgfut6,
  RTRIM(FGFUT7) AS fgfut7,
  RTRIM(FGFUT8) AS fgfut8,
  FGFUT9 AS fgfut9,
  FGFUT10 AS fgfut10,
  FGFUT11 AS fgfut11
FROM LGDAT.BOLD
WHERE "FGBOL#" IN (SELECT "FEBOL#" FROM changed)
