Solved

Oracle sql question

Posted on 2016-07-20
9
69 Views
Last Modified: 2016-08-03
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
0
Comment
Question by:codedigger
  • 4
  • 4
9 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41721463
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

0
 
LVL 32

Expert Comment

by:awking00
ID: 41721490
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
0
 

Author Comment

by:codedigger
ID: 41721499
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?
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41721506
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

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:codedigger
ID: 41721517
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

0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 41721546
the count in having count isn't counting that column over the entire result set.

You can see that by adding the count to the select and removing the having.

If the count column is always 1 based on that group by, the having will never return data.
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,
count(dv.donor_id)
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

Open in new window




See if this is what you are after(I shortened the columns and changed the table names for my test case:
select donor_id, unit_number, fullname from (
select 
	dv.donor_id, 
	dv.unit_number,
	dn.fullname,
	count(dv.donor_id) over(partition by dv.donor_id) visit_count
from
	mydonor_visit dv,
	mydonor dn
where 
	dn.donor_id = dv.donor_id
)
where visit_count>1
/

Open in new window

0
 

Author Comment

by:codedigger
ID: 41721627
slightvw, right on pro, many thanks, I'll adjust my code accordingly and go from there.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41721634
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.
0
 

Author Closing Comment

by:codedigger
ID: 41741337
Sorry, I didn't realize that this question was still open. many thanks slightwv.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now