Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL in oracle

Posted on 2014-02-04
14
Medium Priority
?
257 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
[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
  • 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

715 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