Solved

Oracle sql question

Posted on 2016-07-20
9
78 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:Roberto Madro R.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 77

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:Roberto Madro R.
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 77

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
 

Author Comment

by:Roberto Madro R.
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 77

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:Roberto Madro R.
ID: 41721627
slightvw, right on pro, many thanks, I'll adjust my code accordingly and go from there.
0
 
LVL 77

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:Roberto Madro R.
ID: 41741337
Sorry, I didn't realize that this question was still open. many thanks slightwv.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Recursion 6 34
question about results where i dont have a match 3 36
What are the limitations of input parameters in oracle ? 5 70
SQL query 7 20
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

730 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