WITH changed AS (
    SELECT DIGITS(dcord#) AS ordkey FROM lgdat.qcrh WHERE dcudat BETWEEN '{qtnote_wm}' AND CURRENT_DATE
    UNION
    SELECT DIGITS(dcord#) AS ordkey FROM lgdat.qcrh WHERE dcodat BETWEEN '{qtnote_wm}' AND CURRENT_DATE
    UNION
    SELECT DIGITS(dcord#) AS ordkey FROM lgdat.qcrh WHERE dccdat BETWEEN '{qtnote_wm}' AND CURRENT_DATE
)
SELECT
  RTRIM(GGKEY) AS ggkey,
  "GGPAG#" AS "ggpag#",
  "GGLIN#" AS "gglin#",
  RTRIM(GGTEXT) AS ggtext,
  RTRIM(GGFUT1) AS ggfut1,
  RTRIM(GGFUT2) AS ggfut2,
  RTRIM(GGFUT3) AS ggfut3,
  RTRIM(GGFUT4) AS ggfut4,
  RTRIM(GGFUT5) AS ggfut5,
  SUBSTR(n.GGKEY, 1, 9) AS ggord
FROM LGDAT.QTNOTE n
INNER JOIN changed c ON SUBSTR(n.GGKEY, 1, 9) = c.ordkey
