Solved

SQL in oracle

Posted on 2014-02-04
14
254 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
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 77

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 77

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 77

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 77

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
Extract Currency data from a string and put them in a new field 3 47
migration MS SQL database to Oracle 30 71
oracle numeric condition check 4 34
Error in creating a view. 8 31
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 …
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…
This video shows how to recover a database from a user managed backup
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

679 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