Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL in oracle

Posted on 2014-02-04
14
Medium Priority
?
258 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 2000 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 11

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
Independent Software Vendors: 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!

 
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 78

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 32

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
 
LVL 78

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 32

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 32

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 78

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 78

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

927 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