Solved

Oracle sql question

Posted on 2016-07-20
9
65 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 31

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Query question 4 30
Oracle regular expression 6 30
What is the version of ojdbc6.jar 2 23
SQL Query to display duplicates ? 6 24
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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to recover a database from a user managed backup

758 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