Last active
March 26, 2024 00:58
-
-
Save BirgittaHauser/afffab22887b34e292af01ae07c92413 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Question in an Forum: I have a table/file which has duplicate records (Lets say there are 10 fields in a record). | |
-- Out of which, if 5 fields put together are same across more than 1 record, then it is considered as a duplicate). | |
-- I need to delete those duplicate records alone using a SQL statement embedded in a SQLRPGLE program. | |
-- 1. Solution with a Cursor in embedded SQL (using a Common Table Expression for determining the duplicate rows!) | |
**Free | |
//--------------------------------------------------------------------------------------------- | |
CTl-Opt DatFmt(*ISO) Option(*NoDebugIO); | |
//--------------------------------------------------------------------------------------------- | |
DCL-S GblRRN Int(20); | |
//--------------------------------------------------------------------------------------------- | |
Exec SQL Set Option Commit=*NONE, DatFmt=*ISO; | |
//--------------------------------------------------------------------------------------------- | |
*INLR = *On; | |
Monitor; | |
Exec SQL | |
Declare CsrDupRow Cursor For | |
With x as (Select Key1, Key2, Key3, Key4, Key5, Max(RRN(a)) MaxRRN | |
From MyTable a | |
Group By Key1, Key2, Key3, Key4, Key5 | |
Having Count(*) > 1) | |
Select RRN(b) | |
from x join MyTable b on x.Key1 = b.Key1 | |
and x.Key2 = b.Key2 | |
and x.Key3 = b.Key3 | |
and x.Key4 = b.Key4 | |
and x.Key5 = b.Key5 | |
and rrn(b) <> MaxRRN; | |
Exec SQL Open CsrDupRow; | |
If SQLCODE < *Zeros; | |
//Handle Error | |
Return; | |
EndIf; | |
DoU 1=0; | |
Exec SQL Fetch Next from CsrDupRow into :GblRRN; | |
If SQLCODE < *Zeros; | |
//Handle Error | |
Leave; | |
ElseIf SQLCODE = 100; | |
Leave; | |
EndIf; | |
Exec SQL Delete from MyTable b Where RRN(b) = :GblRRN; | |
If SQLCODE < *Zeros; | |
//Handle Error | |
EndIf; | |
EndDo; | |
Exec SQL Close CsrDupRow; | |
On-Error; | |
//Handle Error | |
EndMon; | |
Return; | |
-- 2. Solution: Handle everything in a DELETE Statement | |
-- Unfortunately Common Table Expressions cannot be used within a DELETE Statement, but nested sub-Selects can: | |
Delete from MyTable c | |
Where rrn(c) in (Select RRN(b) rrnDlt | |
from (Select Key1, Key2, Key3, Key4, Key5, | |
Max(RRN(a)) MaxRRN | |
from MyTable a | |
Group By Key1, Key2, Key3, Key4, Key5) x | |
join MyTable b on x.Key1 = b.Key1 | |
and x.Key2 = b.Key2 | |
and X.Key3 = b.Key3 | |
and x.Key4 = b.Key4 | |
and x.Key5 = b.Key5 | |
and RRN(b) <> MaxRRN); |
Yes, I did!
Fixed it!
Thank you
Mit freundlichen Grüßen / Best regards
Birgitta Hauser
Modernization – Education – Consulting on IBM i
"Shoot for the moon, even if you miss, you'll land among the stars." (Les Brown)
"If you think education is expensive, try ignorance." (Derek Bok)
"What is worse than training your staff and losing them? Not training them and keeping them!"
„Train people well enough so they can leave, treat them well enough so they don't want to.“ (Richard Branson)
From: GlennGundermann ***@***.***>
Sent: Montag, 17. Oktober 2022 15:09
To: BirgittaHauser ***@***.***>
Cc: Birgitta Hauser ***@***.***>; Author ***@***.***>
Subject: Re: BirgittaHauser/Delete Duplicate Rows
@GlennGundermann commented on this gist.
…_____
Are you missing Key4, Key5 on line 68?
—
Reply to this email directly, view it on GitHub <https://gist.github.com/afffab22887b34e292af01ae07c92413#gistcomment-4338465> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/AFPAC75EEFC5LBRM5NAUVYLWDVFYJANCNFSM6AAAAAARHBZIXQ> .
You are receiving this because you authored the thread. <https://github.com/notifications/beacon/AFPAC74P4N42CPZ5NDCUAYDWDVFYJA5CNFSM6AAAAAARHBZIXSWGG33NNVSW45C7OR4XAZNLI5UXG5CDN5WW2ZLOOSVGG33NNVSW45C7NFSM4ACCGMQQ.gif> Message ID: <BirgittaHauser/Delete Duplicate ***@***.*** ***@***.***> >
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Are you missing Key4, Key5 on line 68?