Avatar of Roberto Madro R.
Roberto Madro R.
Flag for United States of America asked on

Oracle sql question

I have a basic query that joins 2 tables (Oracle 9i), it runs fine and all, but I'm trying to fish out duplicate records, and when I resort to having count (column) > 1 the query runs but doesn't return any records.

Is it the fact that I'm joining 2 tables and having count works online on single-table data source, or is there something else at play here.

Thx
Oracle DatabaseSQL

Avatar of undefined
Last Comment
Roberto Madro R.

8/22/2022 - Mon
slightwv (䄆 Netminder)

Shouldn't be the join itself.

Can you post the SQL?

The following example works for me:
drop table tab1 purge;
create table tab1(col1 number);

drop table tab2 purge;
create table tab2(col1 number);

insert into tab1 values(1);
insert into tab1 values(1);
insert into tab1 values(2);

insert into tab2 values(1);
insert into tab2 values(2);
commit;

select t1.col1, t2.col1 from tab1 t1 join tab2 t2 on t1.col1=t2.col1 group by t1.col1, t2.col1 having count(t1.col1) > 1;

Open in new window

awking00

We really need to see your basic query. It could be that the use of having count(column) might be the problem. Typically, you would have something along the lines of -
select a.id, a.colx, b.coly, count(*)  ==> instead count of a certain column
from a inner join b on a.id = b.id
group by a.id, a.colx, b.col
having count(*) > 1
Roberto Madro R.

ASKER
I should've been more precise, I'm not using join per se, but rather selecting from two tables and in the where clause setting  table1.column1 = table2.column1 kind of approach, does that approach factor into the discussion?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
slightwv (䄆 Netminder)

That is still a join.  It is the older syntax.

Updating my query above, still works for me.

select t1.col1, t2.col1 from tab1 t1, tab2 t2 where t1.col1=t2.col1 group by t1.col1, t2.col1 having count(t1.col1) > 1;

Open in new window

Roberto Madro R.

ASKER
Maybe there's something I'm missing;

select 
dv.donor_id, 
dv.visit_datetime,
dv.unit_number,
dn.fullname,
dn.address_line1||' '||dn.address_line2||' , '||dn.city||' '||dn.state_cd||' '||dn.zip_cd5 Address, 
'(' ||dn.evening_phone_area ||')' ||' '|| dn.evening_phone_prefix ||' '||dn.evening_phone_number EvePhone,
dn.date_of_birth,
Substr (Months_Between (sysdate, dn.date_of_birth)/12,0,2) Age,
Lower (dn.email_address) Email,
dv.donation_type_cd,
dv.historical_blood_type
from
rptdba.donor_visit dv,
rptdba.donor dn
where 
dn.donor_id = dv.donor_id and
dv.donation_type_cd Like 'T1' and
dv.historical_blood_type like 'O-' and
dv.batch_date >= to_char(to_date('20120101','YYYYMMDD')) and dv.batch_date <= Sysdate and
dv.visit_status_cd Like 'DN' 
Group By
dv.donor_id,
dv.visit_datetime,
dv.unit_number,
dn.fullname,
dn.address_line1,
dn.address_line2,
dn.city,
dn.state_cd,
dn.zip_cd5, 
dn.evening_phone_area,
dn.evening_phone_prefix,
dn.evening_phone_number,
dn.date_of_birth,
dn.date_of_birth,
dn.email_address,
dv.donation_type_cd,
dv.historical_blood_type
Having count(dv.donor_id) > 1

Open in new window

ASKER CERTIFIED SOLUTION
slightwv (䄆 Netminder)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Roberto Madro R.

ASKER
slightvw, right on pro, many thanks, I'll adjust my code accordingly and go from there.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
slightwv (䄆 Netminder)

Note that my count over just counts the total number of visits for a single donor id.  that likely isn't really what you are after but without sample data and expected results, it is the best I can guess from the SQL you posted.

You would want to partition by all the columns used for the 'duplicates'.

It will also return all the rows.  If you only want the true duplicate rows, you likely want ROW_NUMBER instead of COUNT.
Roberto Madro R.

ASKER
Sorry, I didn't realize that this question was still open. many thanks slightwv.