SELECT * FROM OPENQUERY(GPSERVER, '
SELECT
  RTRIM([CUSTNMBR]) AS [custnmbr],
  RTRIM([CPRCSTNM]) AS [cprcstnm],
  RTRIM([TRXSORCE]) AS [trxsorce],
  [DATE1] AS [date1],
  [TIME1] AS [time1],
  [GLPOSTDT] AS [glpostdt],
  [POSTED] AS [posted],
  RTRIM([TAXDTLID]) AS [taxdtlid],
  RTRIM([APTODCNM]) AS [aptodcnm],
  [APTODCTY] AS [aptodcty],
  [APTODCDT] AS [aptodcdt],
  [ApplyToGLPostDate] AS [applytoglpostdate],
  RTRIM([CURNCYID]) AS [curncyid],
  [CURRNIDX] AS [currnidx],
  [APPTOAMT] AS [apptoamt],
  [DISTKNAM] AS [distknam],
  [DISAVTKN] AS [disavtkn],
  [WROFAMNT] AS [wrofamnt],
  [ORAPTOAM] AS [oraptoam],
  [ORDISTKN] AS [ordistkn],
  [ORDATKN] AS [ordatkn],
  [ORWROFAM] AS [orwrofam],
  [APTOEXRATE] AS [aptoexrate],
  [APTODENRATE] AS [aptodenrate],
  [APTORTCLCMETH] AS [aptortclcmeth],
  [APTOMCTRXSTT] AS [aptomctrxstt],
  RTRIM([APFRDCNM]) AS [apfrdcnm],
  [APFRDCTY] AS [apfrdcty],
  [APFRDCDT] AS [apfrdcdt],
  [ApplyFromGLPostDate] AS [applyfromglpostdate],
  RTRIM([FROMCURR]) AS [fromcurr],
  [APFRMAPLYAMT] AS [apfrmaplyamt],
  [APFRMDISCTAKEN] AS [apfrmdisctaken],
  [APFRMDISCAVAIL] AS [apfrmdiscavail],
  [APFRMWROFAMT] AS [apfrmwrofamt],
  [ActualApplyToAmount] AS [actualapplytoamount],
  [ActualDiscTakenAmount] AS [actualdisctakenamount],
  [ActualDiscAvailTaken] AS [actualdiscavailtaken],
  [ActualWriteOffAmount] AS [actualwriteoffamount],
  [APFRMEXRATE] AS [apfrmexrate],
  [APFRMDENRATE] AS [apfrmdenrate],
  [APFRMRTCLCMETH] AS [apfrmrtclcmeth],
  [APFRMMCTRXSTT] AS [apfrmmctrxstt],
  [APYFRMRNDAMT] AS [apyfrmrndamt],
  [APYTORNDAMT] AS [apytorndamt],
  [APYTORNDDISC] AS [apytornddisc],
  [OAPYFRMRNDAMT] AS [oapyfrmrndamt],
  [OAPYTORNDAMT] AS [oapytorndamt],
  [OAPYTORNDDISC] AS [oapytornddisc],
  [GSTDSAMT] AS [gstdsamt],
  [PPSAMDED] AS [ppsamded],
  [RLGANLOS] AS [rlganlos],
  [Settled_Gain_CreditCurrT] AS [settled_gain_creditcurrt],
  [Settled_Loss_CreditCurrT] AS [settled_loss_creditcurrt],
  [Settled_Gain_DebitCurrTr] AS [settled_gain_debitcurrtr],
  [Settled_Loss_DebitCurrTr] AS [settled_loss_debitcurrtr],
  [Settled_Gain_DebitDiscAv] AS [settled_gain_debitdiscav],
  [Settled_Loss_DebitDiscAv] AS [settled_loss_debitdiscav],
  [Revaluation_Status] AS [revaluation_status],
  [DEX_ROW_ID] AS [dex_row_id]
FROM CHG.dbo.RM30201 t
-- RM30201 has no DEX_ROW_TS (only the RM*0101 pair carries one), so there is
-- no single change stamp: DEX_ROW_ID catches inserts but never moves on an
-- update, and the GLPOSTDT / APFRDCDT window catches restated or back-posted
-- applies. APTODCDT belongs to the apply-TO document, so it lags by weeks and
-- is not a driver; DATE1 has 163 rows dated out to year 6201 and is unusable.
--
-- merge_key is (aptodcty, aptodcnm), so the DELETE clears every apply row for a
-- document -- the pull must therefore be document-grain, not row-grain, or a
-- doc holding two apply rows would lose the sibling the predicate missed.
-- ~16k of 1.13M docs carry more than one row. AK1/AK5 lead on
-- APTODCTY, APTODCNM, so the EXISTS is an index seek.
WHERE EXISTS (
    SELECT 1
    FROM CHG.dbo.RM30201 c
    WHERE c.[APTODCTY] = t.[APTODCTY]
      AND c.[APTODCNM] = t.[APTODCNM]
      AND (   c.[DEX_ROW_ID] > {rm30201_id}
           OR c.[GLPOSTDT]  >= ''{rm30201_dt}''
           OR c.[APFRDCDT]  >= ''{rm30201_dt}'')
)
')
