pipekit/config/modules/rm20201.sql
Paul Trowbridge 09056bea9d Sync the RM apply tables, rm30201 incrementally
rm20201 (Applied Open) is 985 rows and stays a full reload. rm30201
(Apply History) was a full pull of 1,149,405 rows taking 19 minutes; it
now moves 661 rows in 6 s and reconcile --quick ties on all 108 metrics.

Neither apply table has DEX_ROW_TS, so rm30101's pattern does not port.
DEX_ROW_ID catches inserts but never moves on an update, so a 7-day
GLPOSTDT/APFRDCDT window carries restated and back-posted applies.
APTODCDT belongs to the apply-to document and lags by weeks; DATE1 has
163 rows dated to year 6201.

The pull is document-grain, not row-grain. merge_key is (aptodcty,
aptodcnm), so the DELETE clears every apply row for a document, and
staging only the row that matched would drop its siblings -- ~16k of
1.13M docs carry more than one. AK1/AK5 lead on those two columns, so
the EXISTS is an index seek.

Uses OPENQUERY pushdown rather than the four-part linked-server name: an
aggregate through the four-part name ran past 2 minutes against 1.3 s
pushed down. Note no apostrophes may appear inside the OPENQUERY string,
comments included.

Unapplies remain uncovered -- they delete the RM30201 row and write one
back to RM20201, and delete-by-key cannot see a row that is gone. The
985-row rm20201 reload is the cross-check.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-18 23:48:22 -04:00

63 lines
2.2 KiB
Transact-SQL

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 [GPSERVER].[CHG].[dbo].[rm20201]