Solved

SQL in oracle

Posted on 2014-02-04
14
249 Views
Last Modified: 2014-02-26
Hello Experts,

I have a table like below :

create table id_test (
id number , name varchar2(20)
) ;

Open in new window


And records as below:

ID NAME
1  sr
1  ds
2 sr 
2 ds
3  sr

Open in new window


where I want to select only id's which have only name as 'sr'.
But as I have 100's of names so cannot put a not in condition to check other names.

The expected result would be like :
ID NAME
3  sr

Open in new window

0
Comment
Question by:deve_thomos
  • 4
  • 4
  • 3
  • +3
14 Comments
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 500 total points
ID: 39834710
try this :

select x.id , x.name
from id_test x,
( select id, count(distinct name ) cc
from id_test
group by id
having count(distinct name) = 1 ) y
where x.name = 'sr'
and x.id = y.id ;

Thanks,
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39834711
It can be done a lot more ways using a correlated sub query or analytical functions etc and i have just posted one for you to test.

Thanks,
0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 39834747
Try
select max(id),name
from id_test
where name='sr'
group by name;

Open in new window

0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39834781
I tested the query which i had given in my post and it works fine.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 39834783
Another vesion using exists is given below and this also works fine for me. Give this a try as well.

select *
from id_test x
where exists ( select 1 from id_test y where x.id = y.id group by id having count( distinct y.name ) = 1 )
and x.name ='sr'

Thanks,
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39835432
Here's a version that only hits the table once.

It has one known flaw if you can have data like:
4  sr
4  sr


select id, name from
(
select id, name, count(*) over(partition by id order by id) rc from tab1
)
where name='sr' and rc=1
/

Open in new window



If you can have duplicate sr's like I mentioned above then try this:
select id, name from
(
select id, name, sum(case when name='dr' then 1 end) over(partition by id order by id) rc from tab1
)
where name='sr' and rc is null
/

Open in new window

0
 
LVL 31

Expert Comment

by:awking00
ID: 39835598
>>The expected result would be like :
ID NAME
3  sr <<
Is that because there are three values of 'sr' or because 3 is the highest id where the values is 'sr'?
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.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39835606
>>Is that because there are three values of 'sr' or because 3 is the highest id where the values is 'sr'?

Here is how I read the requirement:
I need the rows that only have an 'sr' and no 'other' value.

In other words:  all the other id's have some other value with an 'sr'.  I don't believe MAX has anything to do with it.

If the data was:
1 sr
1 xx
2 sr
3 sr
3 qq
4 bb

the result would be:
2 sr
0
 
LVL 31

Expert Comment

by:awking00
ID: 39835607
>>The expected result would be like :
ID NAME
3  sr <<
Another possibility. Is that because id of 3 only has a value of 'sr' while ids 1 and 2 have values of 'sr' and other values as well?
0
 
LVL 31

Expert Comment

by:awking00
ID: 39835626
slightwv, sorry I was typing while you were responding but, as you can see by my last question, I think your reading of the question is correct.
select id from id_test where name = 'sr'
minus
select id from id_test where name != 'sr'
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39835647
I thought of minus as well but then you have to use it to select back against the main table to get the name.  Not you have hit the table 3 times.
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39838315
select i.id, i.name 
from id_name i left join 
  (select id, count(*) c from id_name where name <> 'sr' group by id) ii on (i.id=ii.id)
where i.name='sr' and ii.c is null;

Open in new window

0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39838865
Surrano's post looks like it works but it also accesses the table more than once.
0
 

Author Closing Comment

by:deve_thomos
ID: 39889573
Thanks a ton...
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.

Join & Write a Comment

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

762 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

22 Experts available now in Live!

Get 1:1 Help Now