Home » SQL & PL/SQL » SQL & PL/SQL » Not exists with union query-Help (Oracle 11G)
Not exists with union query-Help [message #648371] Mon, 22 February 2016 08:28 Go to next message
Ratika
Messages: 5
Registered: February 2016
Location: India
Junior Member
Hi Sir

I am new to this forum and having 1yr exp in oracle . Thanks for having this kind of forum.

I want to convert the below query as view. I have tried with minus and outer join but the output didn't come properly.
I have read your forum guidelines, that you need sample but I am sorry sir that I couldn't put any data here because of security breach and business restriction.
Sir if you could give some idea that will help me to rewrite this query as a view.

the commented line as input.

select distinct cpmt.lbl,
cpmt.end_dt
from cpmsr_t cpmt, rctms_t rct
where cpmt.co_id = rct.co_id
and cpmt.lbl = rct.lbl
and rct.stat_cd = 'IN'
and sysdate between cpmt.eff_dt and cpmt.end_dt
--and rct.pg_id = 1010
-- and rct.time_id > 10
--and rct.co_id = 77)
union
(select cpmt.ndc_lbl, cpmt.end_dt,
from cpmsr_t cpmt, pmsr_t pmt
where pmt.lbl = cpmt.lbl
and pmt.prod = cpmt.prod
and pmt.pckg = cpmt.pckg
--AND cpmt.co_id=77
and pmt.elig_stat_cd = 'EL'
and not exists
(select rct.ndc_lbl
from rctms_t rct
where rct.ndc_lbl =cpmt.ndc_lbl
--and rct.pg_id = 1010
--and rct.time_id > 10
--and rct.co_id =77 )
Re: Not exists with union query-Help [message #648372 is a reply to message #648371] Mon, 22 February 2016 08:42 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
Welcome to the forum. Please read our OraFAQ Forum Guide and How to use [code] tags and make your code easier to read
You must have missed the bits regarding formatting code.

What's your problem? All I see is a SELECT statement.
Re: Not exists with union query-Help [message #648373 is a reply to message #648371] Mon, 22 February 2016 08:43 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum

We don't have your tables.
We don't have your data.
We don't know your requirements.
We are unable to contribute to any desired solution.
Re: Not exists with union query-Help [message #648374 is a reply to message #648372] Mon, 22 February 2016 08:57 Go to previous messageGo to next message
Ratika
Messages: 5
Registered: February 2016
Location: India
Junior Member
Sir.

When i convert this as view i need to have pg_id,time_id,co_id should be in select and will give single where condition as whole rather than giving in each select.

select 
select distinct cpmt.lbl,cpmt.end_dt 
 from cpmsr_t cpmt, rctms_t rct
where cpmt.co_id = rct.co_id
  and cpmt.lbl = rct.lbl
  and rct.stat_cd = 'IN'
  and sysdate between cpmt.eff_dt and cpmt.end_dt
--and rct.pg_id = 1010
--and rct.time_id > 10
--and rct.co_id = 77)
union
(select cpmt.ndc_lbl, cpmt.end_dt, 
   from cpmsr_t cpmt, pmsr_t pmt
  where pmt.lbl = cpmt.lbl
    and pmt.prod = cpmt.prod
    and pmt.pckg = cpmt.pckg
  --and cpmt.co_id=77
    and pmt.elig_stat_cd = 'EL'
    and not exists
               (select rct.ndc_lbl
                  from rctms_t rct
                 where rct.ndc_lbl =cpmt.ndc_lbl
                --and rct.pg_id = 1010
                --and rct.time_id > 10
                --and rct.co_id =77 )
Re: Not exists with union query-Help [message #648375 is a reply to message #648374] Mon, 22 February 2016 09:00 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
how are we to know when correct SQL has been posted here since we can NOT run your SQL because we do NOT have your tables or data?

We do NOT know what correct/desired results should be!
Re: Not exists with union query-Help [message #648376 is a reply to message #648374] Mon, 22 February 2016 09:01 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
You say
Quote:
i need to have pg_id,time_id,co_id should be in select

so add those columns to the projection list.

This isn't difficult, you know. Or is it something more complicated?
Re: Not exists with union query-Help [message #648377 is a reply to message #648376] Mon, 22 February 2016 09:06 Go to previous messageGo to next message
Ratika
Messages: 5
Registered: February 2016
Location: India
Junior Member
Yes sir. I have tried when i give the where condition as whole the second part(after union) of query is not giving the desired result
Re: Not exists with union query-Help [message #648378 is a reply to message #648377] Mon, 22 February 2016 09:08 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
I have no idea what you are talking about.

I think you mean that you have a problem with a predicate. That has nothing to do with column projection.

You had better start again, and explain your problem. Please try to use the correct terms.
Re: Not exists with union query-Help [message #648379 is a reply to message #648377] Mon, 22 February 2016 09:13 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Ratika wrote on Mon, 22 February 2016 07:06
Yes sir. I have tried when i give the where condition as whole the second part(after union) of query is not giving the desired result


We have NO idea what the desired results should be, because you have refused to show us what exactly you expect.
Re: Not exists with union query-Help [message #648380 is a reply to message #648379] Mon, 22 February 2016 09:40 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
The problem is the NOT EXISTS sub-query. OP wants to be able to have all the un-commented bits in a view and the commented bits in the where clause of the select against the view:
e.g.
SELECT * 
FROM view
WHERE pg_id = 1010
AND time_id > 10
AND co_id = 77


When you write a where clause against a view you're effectively taking the output of an existing query and applying further restrictions to it. This can't work for the sub-query as nothing it selects appears in the output of the query.

@Ratika - Do you really need those restrictions on the aub-query? Aren't you just trying to find, in the 2nd part, records in cpmsr_t that do join to pmsr_t but don't have a corresponding record in rctms_t?
In other words does this give the same output as your original query:
select distinct cpmt.lbl,
cpmt.end_dt 
from cpmsr_t cpmt, rctms_t rct
where cpmt.co_id = rct.co_id
and cpmt.lbl = rct.lbl
and rct.stat_cd = 'IN'
and sysdate between cpmt.eff_dt and cpmt.end_dt
and rct.pg_id = 1010
and rct.time_id > 10
and rct.co_id = 77)
union
(select cpmt.ndc_lbl, cpmt.end_dt, 
from cpmsr_t cpmt, pmsr_t pmt
where pmt.lbl = cpmt.lbl
and pmt.prod = cpmt.prod
and pmt.pckg = cpmt.pckg
AND cpmt.co_id=77
and pmt.elig_stat_cd = 'EL'
and not exists
(select rct.ndc_lbl
from rctms_t rct
where rct.ndc_lbl =cpmt.ndc_lbl)
Re: Not exists with union query-Help [message #648385 is a reply to message #648380] Mon, 22 February 2016 14:48 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
try
create or replace my_view as
select  cpmt.lbl,cpmt.end_dt,cpmt.co_id,null elig_stat_cd,null ndc_lbl
from cpmsr_t cpmt, rctms_t rct
where cpmt.co_id = rct.co_id
and cpmt.lbl = rct.lbl
and rct.stat_cd = 'IN'
and sysdate between cpmt.eff_dt and cpmt.end_dt
and rct.pg_id = 1010
and rct.time_id > 10
union
select cpmt.ndc_lbl, cpmt.end_dt,cpmt.co_id,pmt.elig_stat_cd,rct.ndc_lbl
from rctms_t rct,cpmsr_t cpmt, pmsr_t pmt
where pmt.lbl = cpmt.lbl
and pmt.prod = cpmt.prod
and pmt.pckg = cpmt.pckg
and rct.ndc_lbl(+) =cpmt.ndc_lbl;


and use it like this

select lbl,end_dt
from my_view
where co_id = 77
and ndc_lbl is null;


This is untested code. Modify if it doesn't work. The UNION suppresses duplicates so you don't need the distinct.
Re: Not exists with union query-Help [message #648501 is a reply to message #648385] Wed, 24 February 2016 05:27 Go to previous messageGo to next message
Ratika
Messages: 5
Registered: February 2016
Location: India
Junior Member
Thank you sir. I have completed this by creating function.

[Updated on: Wed, 24 February 2016 05:28]

Report message to a moderator

Re: Not exists with union query-Help [message #648507 is a reply to message #648501] Wed, 24 February 2016 06:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68644
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Post the solution you found to help future readers.

Re: Not exists with union query-Help [message #648673 is a reply to message #648507] Mon, 29 February 2016 05:29 Go to previous messageGo to next message
Ratika
Messages: 5
Registered: February 2016
Location: India
Junior Member
I just the passed the arguments
Re: Not exists with union query-Help [message #648675 is a reply to message #648673] Mon, 29 February 2016 05:37 Go to previous message
Michel Cadot
Messages: 68644
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Show us.
Post it.

Previous Topic: html character to unicode
Next Topic: Account Lock ~ OS user
Goto Forum:
  


Current Time: Wed Apr 24 01:02:44 CDT 2024