მიუხედავად დიდი ყურადღებისა არის ხოლმე შეგნებული თუ თვითნებური შემთხვევები როდესაც ცხრილში ხდება ჩანაწერის დუბლირება. განვიხილოთ ამოცანა რომელიც ითვალისწინებთ დუბლირებულ იჩანაწერების მოშორებას ცხრილიდან
ავიღოთ ცხრილი
[cc lang=”sql”]
create table DUBL_TEST
(
FNAME VARCHAR2(200),
AGE NUMBER,
BIRTHDAY DATE
)
[/cc]
და ჩავსვათ მონაცემები შემდეგი სახით
[cc lang=”sql”]
aleko | 25 | 19/11/2007
aleko | 25 | 19/11/2007
nino | 27 | 13/11/2007
nino | 27 | 13/11/2007
aleko | 20 | 19/11/2007
[/cc]
ეხლა ამ ცხრილიდან ამოვიღოთ ის ჩანაწერები რომელსაც გააჩნია დუბლიკატები
[cc lang=”sql”]
select fname, age, birthday from (
select a1.* from dubl_test a1, dubl_test a2 where
a1.fname=a2.fname and a1.age=a2.age and a1.birthday=a2.birthday and a1.rowid<>a2.rowid
) group by fname, age, birthday
[/cc]
შედეგად ჩვენ მივიღეთ ესეთი რეზულტატი –
[cc lang=”sql”]
aleko | 25 | 19/11/2007
nino | 27 | 13/11/2007
[/cc]
ყველაფერი სწორია. ეხლა ამოვიღოთ ის ჩანაწერები რომლებსაც ჩვენ DUBL_TEST ცხრილში არ გააჩნია დუბლიკატი ჩანაწერები
[cc lang=”sql”]
select fname, age, birthday from dubl_test where
rowid not in
(select a1.rowid from dubl_test a1, dubl_test a2 where
a1.fname=a2.fname and a1.age=a2.age and a1.birthday=a2.birthday and a1.rowid<>a2.rowid)
[/cc]
ესეც ესე… ე.ი. ჩვენ მივიღეთ 2 ცხრილი სადაც პირველში დუბლიკატებისგან გაცხრილული ჩანაწერები და მეორეში ის ჩანაწერები რომლებსაც უბრალოდ არ გააჩნდა დუბლიკატები. ეხლა მივიღოთ ეს ორი ცხრილისგან 1 ცხრილი რომელიც იქნება საბოლოო რეზულტატი ჩვენი ამოცანისა –
[cc lang=”sql”]
select fname, age, birthday from (
select a1.* from dubl_test a1, dubl_test a2 where
a1.fname=a2.fname and a1.age=a2.age and a1.birthday=a2.birthday and a1.rowid<>a2.rowid
) group by fname, age, birthday
union all
select fname, age, birthday from dubl_test where
rowid not in
(select a1.rowid from dubl_test a1, dubl_test a2 where
a1.fname=a2.fname and a1.age=a2.age and a1.birthday=a2.birthday and a1.rowid<>a2.rowid)
[/cc]
შედეგი –
[cc lang=”sql”]
aleko | 25 | 19/11/2007
nino | 27 | 13/11/2007
aleko | 20 | 19/11/2007
[/cc]