Thursday, December 03, 2015

QUERY For incorrect sequence of ledger entries (SB)

If its for single account

DECLARE @id INT
SET              @id = 0
                          UPDATE    DCL.ledger                         
SET              @id = seqno = @id + 1
where acctno=321001

For all accounts at once,

 DECLARE @id INT
SET              @id = 0
                          UPDATE    DCL.ledger                          
SET              @id = seqno = @id + 1
where acctno IN  (select acctno  from (select distinct acctno from dcl.ledger group by acctno having count(seqno) <> count(distinct seqno) and acctno in (select acctno from dcl.indexreg where acstatus in ('o','ti','s','f','p')) union select distinct(acctno) from dcl.ledger l where seqno=(select MAX(seqno) from dcl.ledger where acctno=l.acctno group by acctno) and lotdate <> (select max(lotdate) from dcl.ledger where acctno=l.acctno group by acctno) and acctno in (select acctno from dcl.indexreg where acstatus in ('o','TI','s','f','p'))) y )