Solved

Oracle sql question

Posted on 2016-07-20
9
85 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

724 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