EXISTS Vs IN clause usage in query

Hi,
Can anyone please tell me how to rewrite the same query using IN clause instead of EXIST( NOT EXIST) clause.
I guess use of IN operator instead of EXIST in this case would certainly improve query performance as the selective predicates are in subquery and not in Parent query.
This is the query :
SELECT pva.price_dt,
pva.sec_id,
pva.vendor_product_id,
pva.price_typ,
pva.price,
pvp.curcy_cd,
pvp.precedence,
pvp.exch_cd,
pvp.price_srce_cd,
psq1.scrub_queue_id,
pva.prc_derivation_cd,
'Alternate Price' as location
FROM prc_vendor_price pvp,
prc_vendor_alternate_price pva,
prc_scrub_queue psq1
WHERE psq1.sec_id=pva.sec_id
AND psq1.sec_id=pvp.sec_id
AND pva.sec_id=pvp.sec_id
AND psq1.eff_dt=pva.price_dt
AND psq1.eff_dt=pvp.price_dt
AND pva.price_dt=pvp.price_dt
AND pvp.prc_derivation_cd=pva.prc_derivation_cd
AND pva.vendor_product_id=pvp.vendor_product_id
AND NOT EXISTS (SELECT 1
FROM prc_vendor_price pvp2
WHERE pva.sec_id=pvp2.sec_id
AND pva.price_dt=pvp2.price_dt
AND pva.price=pvp.price
AND pva.price_typ=pvp2.price_typ
AND pva.prc_derivation_cd=pvp2.prc_derivation_cd
AND pva.vendor_product_id=pvp2.vendor_product_id)
I know how to do it for query with single table but dont know how to do it for multiple tables and joins so please gimme some hint.
Thanks,
Aashish

How about not using NOT IN or NOT EXISTS...
SELECT pva.price_dt,
       pva.sec_id,
       pva.vendor_product_id,
       pva.price_typ,
       pva.price,
       pvp.curcy_cd,
       pvp.precedence,
       pvp.exch_cd,
       pvp.price_srce_cd,
       psq1.scrub_queue_id,
       pva.prc_derivation_cd,
       'Alternate Price' as location
FROM prc_vendor_price pvp JOIN prc_vendor_alternate_price pva ON (pva.sec_id=pvp.sec_id
                                                              AND pva.price_dt=pvp.price_dt
                                                              AND pva.vendor_product_id=pvp.vendor_product_id
                                                              AND pvp.prc_derivation_cd=pva.prc_derivation_cd)
                          JOIN prc_scrub_queue psq1 ON (psq1.sec_id=pva.sec_id
                                                    AND psq1.sec_id=pvp.sec_id
                                                    AND psq1.eff_dt=pva.price_dt
                                                    AND psq1.eff_dt=pvp.price_dt)
               LEFT OUTER JOIN prc_vendor_price pvp2 ON (pva.sec_id=pvp2.sec_id
                                                     AND pva.price_dt=pvp2.price_dt
                                                     AND pva.price=pvp.price
                                                     AND pva.price_typ=pvp2.price_typ
                                                     AND pva.prc_derivation_cd=pvp2.prc_derivation_cd
                                                     AND pva.vendor_product_id=pvp2.vendor_product_id)
WHERE pvp2.sec_id is null

Similar Messages

  • Where Exist clause to improve query performance

    select * from emp
    where emp_code in (select emp_code from emp_acct)
    it is said tht its always better to use where exist clause instead of IN.
    I hav written the same query using where Exist,
    select * from emp e
    where exists (select null from emp_acct ea where e.emp_code = ea.emp_acct)
    but both these queries are sharing the same cost.
    is there is any other way to use exist to decrease cost of the query.
    ---Piyush

    You can't compare the cost of two different queries.
    You can't relate cost to the running time of the query.
    Having said that, why is this not just a join?
    select e.*
    from emp e
    , emp_acct ea
    where e.emp_code = ea.emp_codecheers,
    Anthony

  • In Oracle, Can i use if exits clause in a query?

    In Oracle, Can i use if exits clause in a query?
    For example, "Drop table if exists tablename"
    Is the above command valid in oracle?
    If not then is there any equivalent for if exists clause?

    Here is the SP code code that might help you to Drop a table if it exisit, whith out throwing an error if the table is not present.
    create or replace PROCEDURE DROP_TABLE(TabName in Varchar2)
    IS
    temp number:=0;
    tes VARCHAR2 (200) := TabName;
    drp_stmt VARCHAR2 (200):=null;
    BEGIN
    select count(*) into temp from user_tables where TABLE_NAME = tes ;
    if temp =1 then
    drp_stmt := 'Drop Table '||tes;
    EXECUTE IMMEDIATE drp_stmt;
    end if;
    EXCEPTION
    WHEN OTHERS THEN
    raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
    END DROP_TABLE;
    Call this SP in your Scripts by : CALL DROP_TABLE ('<Table Name>')
    Hope this Helps!
    Regards,
    Kaarthiik

  • Suppress "Order By" clause in Answers Query

    Hello,
    Is it possible to Suppress "Order By" clause in Answers Query.
    I'm using a database view as data source. In the view definition, "order by" clause is already specified. Is it possible to get the same order in the OBIEE report??
    I do not want to use Sort Order column in the repository.
    Thanks,
    Girish

    You add a rownumber to your DB view and use that to 'sort' your report.
    regards
    John
    http://obiee101.blogspot.com/

  • How do you use 3 Where Clauses in a query

    Hi, i am trying to figure out how to use 3 Where Clauses in a Query where 2 of the Where Clauses uses a Sub query.
    Display the OrderID of all orders that where placed after all orders placed by “Bottom-Dollar Markets”.
    Order the result by OrderID in ascending order.
    First WHERE clause checks for OrderDate and uses a sub query with ALL keyword.
    Second WHERE clause use equals and sub query.
    Third WHERE clause uses equal and company name.
    This is what i have so far but i am pretty confused on how to do this.
    My Code for NorthWind:
    Select OrderID
    From Orders o
    Where o.OrderID IN (Select OrderDate From Orders Where Orders.OrderID > ALL
    (Select CompanyName From Customers Where CompanyName = 'Bottom-Dollar Markets'));
    The book shows how to use the ALL Keyword but not in a Sub query with Multiple Where Clauses.
    Select VenderName, InvoiceNumber, InvoiceTotal
    FROM Invoices JOIN Vendors ON Invoices.VendorID = Vendors.VendorID
    WHERE InvoiceTotal > ALL (Select InvoiceTotal From Invoices Where VendorID = 34)
    ORDER BY VendorName;

    >Where Orders.OrderDate
    > ALL  (Select
    CompanyName
    The comparison operator (>) requires compatible data types.
    DATETIME is not compatible with VARCHAR string for comparison.
    Here is your homework:
    SELECT orderid
    FROM orders o
    WHERE o.orderdate > ALL (SELECT orderdate
    FROM orders
    WHERE shipvia = (SELECT Max(shipvia)
    FROM orders o
    INNER JOIN customers c
    ON c.customerid =
    o.customerid
    WHERE
    c.companyname = 'Bottom-Dollar Markets'));
    11064
    11065
    11066
    11067
    11068
    11069
    11070
    11071
    11072
    11073
    11074
    11075
    11076
    11077
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Database Design
    New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014

  • With clause in SQL query data model

    Hello!
    isn't it possible to use the with clause in sql query data models?
    for example following query:
    WITH
    a_test as (
    select dummy from dual
    select *
    from a_test
    brings up a "XML Parsing Error: no element found"-error...
    BR Paul

    I tried a slightly different query (see below) and the query worked fine and retrieved data.. I did not get any errors.
    WITH
    a_test as (
    select 'dummy' from dual
    select *
    from a_test
    This works as well.. retrieving the value of the parameter
    WITH
    a_test as (
    select :Test_ID from dual
    select *
    from a_test
    thanks,
    BIPuser

  • Order by clause with Named Query

    hi
    i have to give order by clause in Named Query
    how we have to specify is can any body help
    thanks
    Harish

    Assuming an Entity called Handset:
    select h from Handset h order by h.description

  • Order by clause in Sub query

    Hi,
    Can we use order by clause in Sub query?
    While using the order by clause, I am getting the "missing expression error" . If I remove order by clause query executing fine.
    Here is my query:
    select *
    from emp_mstr
    where emp_no in(select
    emp_no
    from emp_mstr
    order by branch_no);
    Thanks & Regards,
    Mahi

    May be you miss some required spaces also, other than wrong use of ORDER BY
    select *
    from emp_mstr
    where emp_no in
         ( select e2.emp_no
           from emp_mstr e2
    --       order by e2.branch_no
         );Why do you want to ORDER BY in the subquery, which you use with IN clause? That will not make any difference in the result..Means the result you get with ORDER BY will be same as without that.. And in this case, ORDER by is a unncessary overhead.. And Ordering is very costly..
    And why do you want to have the IN clause at all in your query? You are referring the same tables in the main query and sub query..
    The below will give the same result
    select *
    from emp_mstr
    where emp_no is not nullIf you want to use another table in the subquery, always use aliasess...
    select *
    from emp_mstr
    where emp_no in
         ( select e2.emp_no
           from emp_mstr2 e2
    --       order by e2.branch_no
         );

  • Regarding  dynamically assigning the where clause to select query

    hi,
      Please send the code regarding how to dynamically assign the where clause to select query.
    thanks in advance

    SELECT <fileds>
            INTO TABLE itab
            FROM dbase
            WHERE  condition.

  • How to change where clause in VO query in bean

    Hi experts
    I have to modify vo object query where clause on run time .. is it possible??
    if yes please suggest me,

    as mentioned by Timo, it is very much possible
    see this following link -
    Changing the WHERE clause or VO Query at runtime in Oracle ADF | Techartifact
    Dynamically changing query in view object in Oracle ADF | Techartifact

  • IS IT POSSIBLE TO  WRITE ORDER BY CLAUSE WITHIN INNER QUERY

    IS IT POSSIBLE TO WRITE ORDER BY CLAUSE WITHIN INNER QUERY

    So you still can't :) I still don't see it that strict:
    You know of course that this is possible:
    select ename, (select ename
                     from (select   empno, ename
                               from emp
                              where deptno = 10
                           order by 1) e2
                    where e.empno = e2.empno) a
      from emp eso we have an »ORDER BY CLAUSE WITHIN INNER QUERY« which is even correlated (though through the outer query).
    Whether this makes sense or not is not question imho :-) ... but you can

  • Let us discussion "non recursive with clause" usage

    I think there are 3 "non recursive with clause" usage.
    My question is do you know more "non recursive with clause" usage ?

    Another option is to use it to materialize remote data on the fly. Especially in combination with the materialize hint.
    I think I used this tecnique once, but can't find the proper example anymore. Very simplified it could looked like this:
    with fetchData as (Select /*+materialize */ * from myremoteTable@databaselink where status = 'CURRENT')
    select *
    from fetchdata r
    full outer join localData l on r.id = r.id
    where l.status = 'CURRENT'
    ;From 11g onwards: use the with clause to create better column names in larger select from dual combinations.
    Not sure with that results in a suitable use case.
    So instead of
    with orders as
    (select 1 id , 173 order#, 'John' customer, 'America' region from dual union all
      select 2 id , 170 order#, 'Paul' customer, 'UK' region from dual union all
      select 3 id , 240 order#, 'Hans' customer, 'Europe' region from dual union all
      select 4 id , 241 order#, 'Francois' customer, 'Europe' region from dual )
    select * from orders;you can now write
    with
    orders (id, order#, customer,region) as
    (select 1 , 173 , 'John' , 'America' from dual union all
      select 2 , 170 , 'Paul' , 'UK' from dual union all
      select 3 , 240 , 'Hans' , 'Europe' from dual union all
      select 4 , 241 , 'Francois' , 'Europe' from dual )
    select * from orders;THis makes it a little easier to create tase data useing some excel sheet I guess.

  • Exists clause in plsql query

    HI I see that by the use of exists clause while joining
    1) oralce provides a better execution plan ... and 2) it eliminates duplicates automatically
    but my question is how to access the columns of the inner tables in the select clause ?
    EG:
    select a.sno, b.course_name
    from a,b
    where a.cid=b.cid
    ----------------------in the below query How do i access b.course_name in the select clause? similarly even if it contains more tables nested, i should be able to access the column of the inner tables from the select clause . How to do this . hope i am clear
    select a.sno from a where exists ( select 1 from b where b.cid=a.cid)

    You cannot access columns from tables in an EXISTS (or NOT EXISTS) clause outside of that clause. You can only select data from tables (and other objects) that you are actually selecting from.
    Justin

  • Dynamic where clause in my query

    I am using a view
    select * from vw_pt_inv_customer
    My requirement is have a web page where users can search for customers by filling in a form which has the following feilds: - User can fill in the form with all the feilds or not. I want a dynamic where clause.. how can i get this work for me?
    Title
    Firstname
    Surname
    Address1
    Address2
    Postcode
    Telephone No

    879796 wrote:
    I am using a view
    select * from vw_pt_inv_customer
    My requirement is have a web page What web frame work is used? Apex? Something else?
    where users can search for customers by filling in a form which has the following feilds: - User can fill in the form with all the feilds or not. I want a dynamic where clause.. how can i get this work for me?Dynamic where clauses are a Very Bad Idea (tm).
    And having an open ended search function on a web page is also not a great idea.
    If you are using Apex, the for performance (and even query flexibility) it will be better creating a separate reporting region for each unique query. A boolean rendering condition checks the existing bind variables in order to determine if that specific reporting region should be executed.
    This results in a reporting region having a proper and dedicated SQL query (no hacking of the where clause) and that region only being rendered for the proper combination of supplied filter criteria.
    A slightly more complex, but more flexible approach, is using a generic reporting region that calls a PL/SQL function that creates returns the SQL query for execution and rendering. The approach to this is very similar to constructing a SQL ref cursor (dynamically) for a client. The only difference is that instead of creating the actual ref cursor, the code simply needs to return the SQL source code statement - with bind variables. The Apex run-time does the rest (does the binds and execution and rendering).
    If you are not using Apex - you should still consider these approaches. And not hacking a single SQL to cater for all different types of filter conditions.

  • GROUP BY and DISTINCT clause in single query

    Hi All,
    I have this query:
    SELECT studyid,baseline_no,trans_date, min(trunc(compass_date)),drug_related_yn
    FROM cp_bdr_trigger_tbl cbtt
    WHERE NOT EXISTS (SELECT 1
    FROM cp_patient_info_tbl
    WHERE studyid = cbtt.studyid
    AND baseline_no = cbtt.baseline_no)               
    AND studyid = '0431-020'
         GROUP BY BASELINE_NO,STUDYID,DRUG_RELATED_YN;
    The o/p of this query has around 20 rows with same studyid and distinct baseline_no.
    Now I want to select the data all to be distinct by limiting it somehow by GROUP BY clause.
    Is this possible?
    I mean grouping should be done in such a way that it should return distinct values only...
    The o/p of this query is input to other query for insertion in some table.
    * there is a Unique index in the target table on studyid+basline_no combination so the combination has to be unique*
    Thanks,
    Aashish S.

    The GROUP BY is in a way an implied DISTINCT because it will return ONE record for each of the columns identified in the GROUP BY clause.
    Either way your requirements aren't very clear. If you can please post the following we may be able to help:
    1. Oracle version (e.g. 10.2.0.4)
    2. CREATE / INSERT statements with sample data
    3. Expected output
    4. Use \ tags (surround #2 and #3 in these tags)
    5. Explanation of logic in achieving #3.
    Thanks!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Maybe you are looking for