Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Exists functionality

576659Apr 11 2008 — edited Apr 11 2008
Hi all
I have a table called wood and it has the following details

fund pricetype source
----------------------------------------
F1 WM A
F1 WM B
F1 VMS A
F2 WM A
F2 WM B
F3 WM A
F4 VMS A

I want the funds which has pricetype only ='WM' using exist function
for fund F1 the price type is both 'WM' and 'VMS' so this shoud not come.
The result shud beF2 and F3

Thanks
Hari

Message was edited by:
Hariharan M K

Comments

Sven W.
One possible solution (there are many others)
with wood as (
select 'F1' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F1' fund, 'WM' pricetype, 'B' source from dual UNION ALL
select 'F1' fund, 'VMS' pricetype, 'A' source from dual UNION ALL
select 'F2' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F2' fund, 'WM' pricetype, 'B' source from dual UNION ALL
select 'F3' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F4' fund, 'VMS' pricetype, 'A' source from dual)
select fund 
from
	(select fund, decode(pricetype,'WM',1,0) type_wm, decode(pricetype,'WM',0,2) type_other
	from wood 
	group by fund, pricetype
	)
group by fund
having sum(type_wm+type_other) = 1;
I didn't use exists since that would require to access the same table twice.
576659
hi sven,\
Thanks for your reply..
But i need to use Exist function for this query
Thanks
Hari
584412
Then what does your teacher say? Presumably he / she gave you notes on how to use EXISTS?
Nicolas Gasparotto
But i need to use Exist function for this query
Why ? Did you tried something else to start ?

Nicolas.
576659
i am asking you to help me out.....Please help me
Nicolas Gasparotto
Please, explain why the Sven's query is not good enough for you.
And make effort on your side by trying to write the query first.

Nicolas.
user627955
with wood as (
select 'F1' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F1' fund, 'WM' pricetype, 'B' source from dual UNION ALL
select 'F1' fund, 'VMS' pricetype, 'A' source from dual UNION ALL
select 'F2' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F2' fund, 'WM' pricetype, 'B' source from dual UNION ALL
select 'F3' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F4' fund, 'VMS' pricetype, 'A' source from dual)
select *
from wood
where pricetype = 'WM'
and not exists (select 1 from wood wood_inner where wood_inner.fund = wood.fund and pricetype != 'WM');
584412
Well done, you've now completed his homework assignment for him. And when he goes looking for a job, and gets one where he'll have to actually perform such tasks, he will still be clueless, but hey, you can still do his work for him.
Nicolas Gasparotto
Does NOT EXISTS same as EXISTS ?
;-)

Nicolas.
Sven W.
I guess NOT.
576659
Thank you so much..
and thanks JS1 for your joke :(
576659
hi gasparotto,
I am not saying Svens query is not good.. I am trying that in multiple ways.
I was trying that query from my side also..
Hari
Nicolas Gasparotto
What I was trying to explain : it is good when poster show on the forum their effort to solve their problem on their own side before asking for help here.
Especially when there is some inexplicable restriction on the solution to give.

Nicolas.
576659
Thanks gasparotto for your valuable advice..
Will take it sincerely.
What hurted me was , some of them made fun of me.
Thanks
Hari
Aketi Jyuuzou
with wood as (
select 'F1' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F1' fund, 'WM' pricetype, 'B' source from dual UNION ALL
select 'F1' fund, 'VMS' pricetype, 'A' source from dual UNION ALL
select 'F2' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F2' fund, 'WM' pricetype, 'B' source from dual UNION ALL
select 'F3' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F4' fund, 'VMS' pricetype, 'A' source from dual)
select fund
from wood
group by fund
having min(case when pricetype = 'WM' then 1 else 0 end)=1;
FU
--
F2
F3

If we want other ResultSet,we can use below solution.

with wood as (
select 'F1' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F1' fund, 'WM' pricetype, 'B' source from dual UNION ALL
select 'F1' fund, 'VMS' pricetype, 'A' source from dual UNION ALL
select 'F2' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F2' fund, 'WM' pricetype, 'B' source from dual UNION ALL
select 'F3' fund, 'WM' pricetype, 'A' source from dual UNION ALL
select 'F4' fund, 'VMS' pricetype, 'A' source from dual)
select fund,pricetype,source
from (select fund,pricetype,source,
      min(case when pricetype = 'WM' then 1 else 0 end) over(partition by fund) as willOut
      from wood)
where willOut = 1;
FU  PRI  S
--  ---  -
F2  WM   A
F2  WM   B
F3  WM   A

similar threads(OTN)
585154
551338
621506
634915

similar threads(OTN-Japan)
http://otn.oracle.co.jp/forum/thread.jspa?threadID=35002855
http://otn.oracle.co.jp/forum/thread.jspa?threadID=35003244

Boneist
I don't see anyone making fun of you.

I see people trying to get you to a) explain your reasons for the restriction to use EXISTS, and b) display your attempts at solving the issue at hand.

Your question looks suspiciously like a coursework question, plus you showed no workings of your own, hence the reluctance to hand you the answer on a plate when it looked like you hadn't done anything yourself.

The more you show that you are trying stuff out for yourself, but are stuck on a particular bit, and the more you explain the reasons behind your requirements, then the more people will supply helpful messages. The more you make it look like "do my work for me" then the less likely you'll get helpful suggestions!
1 - 16
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 9 2008
Added on Apr 11 2008
16 comments
2,759 views