Query with union

Hi,
I have three table:
Candidates: (candidate_id primary key)
Purhcase_orders: (Po_number primary key)
Bids: (bid_number primary key)
Just wanted to retrieve candidate information like candidate_id, first_name etc, along with count of purchase orders issued against this candidate and count of bid_number.
For that I have written below query:
query - 1:
SELECT CANDIDATE_ID,
INITCAP (first_name) FIRST_NAME,
+(SELECT COUNT (DISTINCT bid_number)+
FROM bids
WHERE BIDS.CANDIDATE_ID = cand.candidate_id)
BID_COUNT,
+(SELECT COUNT (DISTINCT po_number)+
FROM purchase_orders po
WHERE PO.CONTRACTEE_ID = cand.candidate_id)
PO_COUNT,
+'Y' match_flag,+
DECODE ( (SELECT candidate_id
FROM v_ineligible_candidates vic
WHERE VIC.CANDIDATE_ID = cand.candidate_id),
NULL, 'N',
+'Y')+
INELIGIBLE_FLAG
FROM candidates cand
Based on business requirement, need to create another set of query (query -2) same like above. In that we have some decode statements to calculate match_flag, then need to perform UNION operation between query -1 and query -2.
have around 130563 number of rows in candidates table.
Individually both queries are taking 500 msec.
But when I have used union, it didnt give result for atleast 60 sec. Then I cancelled execution.
Execution plan for the final query with UNION: (final cost)
Cost: 2,873  Bytes: 9,130,170  Cardinality: 260,862                
Please help me in this issue.
Why its taking that much time in union.
Please correct my mistake and suggest me in this query.
UNION ALL is running but not UNION.
Thanks in advance

Please post complete Explain Plans.
To get detailed information of Explain Plan use
explain plan for
your_query;
select *
  from table(dbms_xplan.display(null, null, 'ALL'));Post it alongwith below details between {noformat}{noformat} (exactly as specified) tags to preserve formatting and ease of understanding.
On your requirement,
Using UNIONALL you will get two set of rows, with UNION you might get a single row if your MATCH_FLAG are same for both queries. Use of either, has to meet your requirements than to meet your performance. If it is Business Acceptable, then look for performance and tuning.
If you could do the folllowing, it might be helpful for you.
1. Post the Sample table structure (Table information of Candidates should suffice) in a Create Table/With Sub-Query clause.
2. Sample Data in Insert Into/With Sub-Query format.
3. Expected Outcome based on Sample data
4. The Explain Plan of your Queries as requested above.
Reason of asking these details are, there might be a way to avoid Hitting the table Twice and avoid use of UNION/UNIONALL clauses.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • Select query with UNION clause in database adapter

    Friends,
    I have got a SQL query with two UNION clause withing it. like
    select a,b,c
    from a
    union
    select a,b,c
    from b
    The schema generated is like below in sequence
    <element>a</element>
    <element>b</element>
    <element>c</element>
    <element>a</element>
    <element>b</element>
    <element>c</element>
    So, the columns from different select queries joined with UNION clause are all appeared in schema instead of the distinct columns.
    Is there any way around to solve this issue ? or will need to with DB function/procedure.

    I think I know what you are saying but your example doesn't make sense, your SQL should produce something like
    I had to change a, b, c with elementA, elementB, elementC as a and b are reserved html tags.
    <elementA>DateA</elementA>
    <elementB>DataB</elementB>
    <elementC>DataC</elementC>
    ...What is the result of the query when you run it in SQLPlus? Is it what you expect?
    cheers
    James

  • Query with Union to Show Ship to of Both Supplier & Customer BP's

    Hi Guys,
    I need to do a query that I can use in a formatted search, that will allow me to select from a list that shows both BP Ship to Addresses from The Customer and Supplier Tables. I know I have to use a union, however I have no clue where to start with this - would someone be so kind as to point me in the right directions please.
    Kind regards
    Sean Martin
    Edited by: Sean Martin on Apr 18, 2011 5:20 PM

    Hello Sean - I see you are new - let me welcome you to the SAP B1 forums...
    Maybe you do have the wrong forum as Julie suggests above, but maybe it is a bit perplexing as to what you need in an SAP B1 sense...
    1.  Let us know where you would be using the formatted search (in the Sales documents? - in the Purchasing documents?) - OR - maybe give a business scenario...
    2.  Let us know what field the results would populate...
    3.  Are you looking for two formatted searches - one to be used with suppliers and one to to be used for customers - OR - do you want the formatted search to list all Ship-To addresses combined into one list?
    4.  A statement of UNION might not be needed!
    5.  The tables to reference in the formatted search are OCRD and CRD1.
    That way you can help us help you better...again, wecome and...
    Regards - Zal

  • Query with UNION option

    Hi ,
    I have one partition table.The partitions are created datewise.Now when i have to fire select query on that talble i am firing below querry
    Select count(1) from table A where createdate between ('01-JAN-2012 00:00:00','DD-MON-YYYY HH24:MI:SS') AND ('01-JAN-2012 23:59:59','DD-MON-YYYY HH24:MI:SS')
    Above query will scan 1 day partition.
    now if i want to fetch records from whole month ,it will scan 31 partition one by one.
    Select count(1) from table A where createdate between ('01-JAN-2012 00:00:00','DD-MON-YYYY HH24:MI:SS') AND ('31-JAN-2012 23:59:59','DD-MON-YYYY HH24:MI:SS')
    Now if i change the query by writing another way which will scan partition parallely.
    Select count(1) from table A where createdate between ('01-JAN-2012 00:00:00','DD-MON-YYYY HH24:MI:SS') AND ('01-JAN-2012 23:59:59','DD-MON-YYYY HH24:MI:SS')
    union
    Select count(1) from table A where createdate between ('02-JAN-2012 00:00:00','DD-MON-YYYY HH24:MI:SS') AND ('02-JAN-2012 23:59:59','DD-MON-YYYY HH24:MI:SS')
    union
    Select count(1) from table A where createdate between ('03-JAN-2012 00:00:00','DD-MON-YYYY HH24:MI:SS') AND ('03-JAN-2012 23:59:59','DD-MON-YYYY HH24:MI:SS')
    union
    till
    Select count(1) from table A where createdate between ('31-JAN-2012 00:00:00','DD-MON-YYYY HH24:MI:SS') AND ('31-JAN-2012 23:59:59','DD-MON-YYYY HH24:MI:SS')
    Is it possible that i can make query by passing different month in above union query .
    Month can be dynamic, and above select can be take the values by passing the month.
    kindly guide me
    =======

    I hear you. But can you demonstrate, instead of just telling?
    Else, I'll be guessing, that you are just guessing. And I am inclined to guess that your guess is wrong.
    But, I could be guessing wrong, of course. (And week-ends I am not online to test it myself)
    Start by showing the table, ddl. Is it parallel, noparallel? - And then show the plans for both queries.
    And how did you come to that conclusion of "one by one" vs "in parallel"?
    Regards
    Peter

  • Join query with union

    Hi all
    I have two queries and i want to join this two query
    The report column should be like this
    item_number WK_30  WE_311st query
    select
    re.item_number,
    nvl(le.quantity,0) - nvl(re.quantity,0) WK_30
    from BACKLOG_WEEK_WH_AFTR_ATP le, BACKLOG_ATP_GT_CW_IN re
    where le.item_number =re.item_number
    and to_number(substr(re.year_week,-2,2)) = to_number(to_char(sysdate,'IW'))+12nd query
    select
    re.item_number,
    nvl(le.quantity,0) - nvl(re.quantity,0) WK_31
    from BACKLOG_WEEK_WH_AFTR_ATP le, BACKLOG_ATP_GT_CW_IN re
    where le.item_number =re.item_number
    and to_number(substr(re.year_week,-2,2)) = to_number(to_char(sysdate,'IW'))+2Thanks in advance
    Regards

    Hello
    It sounds like you need a cumulative sum, in which case I think you'll need to go with analytics. If you could supply create table statements, some sample data and a clear example of the output you expect, it would be easier to put something together. However, I think the statement below should work although it is untested.
    SELECT
        item_number,
        plus_1,
        plus_2,
        plus_3,
        plus_4,
        plus_5,
        plus_6,
        plus_7,
        plus_8,
        plus_9,
        plus_10,
        plus_11,
        plus_12,
        plus_13
    FROM
        (   select
                re.item_number,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+1, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_1,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+2, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_2,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+3, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_3,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+4, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_4,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+5, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_5,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+6, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_6,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+7, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_7,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+8, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_8,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+9, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_9,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+10, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_10,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+11, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_11,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+12, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_12,
                sum(decode (to_number(substr(re.year_week,-2,2)), to_number(to_char(sysdate,'IW'))+13, nvl(le.quantity,0) - nvl(re.quantity,0),0))
                OVER( PARTITION BY re.item_number ORDER BY to_number(substr(re.year_week,-2,2)) PLUS_13,
                ROW_NUMBER() OVER(PARTITION BY re.item_number) rn
            from
                BACKLOG_WEEK_WH_AFTR_ATP le, BACKLOG_ATP_GT_CW_IN re
            where
                le.item_number =re.item_number
            and
                and to_number(substr(re.year_week,-2,2)) in
                (   to_number(to_char(sysdate,'IW'))+1,
                    to_number(to_char(sysdate,'IW'))+2,
                    to_number(to_char(sysdate,'IW'))+3,
                    to_number(to_char(sysdate,'IW'))+4,
                    to_number(to_char(sysdate,'IW'))+5,
                    to_number(to_char(sysdate,'IW'))+6,
                    to_number(to_char(sysdate,'IW'))+7,
                    to_number(to_char(sysdate,'IW'))+8,
                    to_number(to_char(sysdate,'IW'))+9,
                    to_number(to_char(sysdate,'IW'))+10,
                    to_number(to_char(sysdate,'IW'))+11,
                    to_number(to_char(sysdate,'IW'))+12,
                    to_number(to_char(sysdate,'IW'))+13
    WHERE
        rn = 1HTH
    David

  • Urgent! How to use push_pred to optimize query with UNION in 10g?

    Hi,
    We are facing slow query performance in 10g database.
    Appreciate if anyone could advise on how to optimize the performance by using push pred?
    Or is there any other ways.
    Thanks in advance.
    Cheers,
    SC

    dont post duplicate post

  • Problems with Union statement

    Hi,
    I'm writing an application with jdbc. I'm using Oracle Release 8.0.5.0.0, j2re 1.4.2_06.
    The query with UNION produces a resultSet with no record while if I execute the same query with Oracle SQL Worksheet I've one record.
    This is the code with a sample query:
    Class.forName(jdbcDriver).newInstance();
    Connection c = DriverManager.getConnection(dbUrl, "**", "**");
    Statement st = c.createStatement();
    String query = "(select fam from people where id=1711) union (select fam from family where fam=397)";
    ResultSet rs = st.executeQuery(query);
    if(rs!=null){
    while(rs.next()){...}
    rs.next is false but I know It's not correct!
    I dont get any exception and until I didn't use the UNION statement all worked fine!
    Please help me, thanks

    ok... I'm replying to myself:
    It works if I write the query as follow:
    "select fam from people where id=1711 union select fam from family where fam=397"
    without '( ... )' !
    bye

  • How do I create a folder or report from a query with a union and parameters

    I have created folders with unions but I am having difficulty coverting a query with a union and parameters.
    The following works great in SQL*Developer without parameters, but I want to change to use parameters for the year and quarter and use it in Discoverer:
    SELECT TO_CHAR(NVL(AV.TAX_ID,999999999),'000000000') FEID,
      AV.FIRM_NAME VENDOR_NAME,
      AV.BIDCLASS CONTRACT_CODES,
      AV.AWAMT AWARD_AMOUNT,
      AV.SOL_MODE FORMAL_INFORMAL,
      AV.CERT BUSINESS_ENTITY,
      AV.ETHNICITY ETHNICTY,
      AV.PO_NUMBER_FORMAT CONTRACT,
      SUM(VP.INVOICE_AMOUNT) AMOUNT_PAID_$
    FROM CONFIRM.VSTATE_PAID_AWARD_VENDORS AV,
      CONFIRM.VSTATE_VENDOR_PAYMENTS VP
    WHERE ( ( AV.PO_NUMBER = VP.PO_NUMBER
    AND AV.VENDOR_ID       = VP.VENDOR_ID ) )
    AND (TO_CHAR(VP.PAYMENT_DATE,'Q') = '4')
    AND ( TO_CHAR(VP.PAYMENT_DATE,'YYYY') = '2009' )
    GROUP BY TO_CHAR(NVL(AV.TAX_ID,999999999),'000000000'),
      AV.FIRM_NAME,
      AV.BIDCLASS,
      AV.AWAMT,
      AV.SOL_MODE,
      AV.CERT,
      AV.ETHNICITY,
      AV.PO_NUMBER_FORMAT
      union
    SELECT TO_CHAR(NVL(AV2.TAX_ID,999999999),'000000000') FEID,
      AV2.FIRM_NAME VENDOR_NAME,
      AV2.BIDCLASS CONTRACT_CODES,
      AV2.AWAMT AWARD_AMOUNT,
      AV2.SOL_MODE FORMAL_INFORMAL,
      AV2.CERT BUSINESS_ENTITY,
      AV2.ETHNICITY ETHNICTY,
      AV2.PO_NUMBER_FORMAT CONTRACT,
      0 AMOUNT_PAID_$
    FROM CONFIRM.VSTATE_PAID_AWARD_VENDORS AV2
    WHERE
    not exists (SELECT 'X'
                  FROM CONFIRM.VSTATE_VENDOR_PAYMENTS VP2
                 WHERE av2.po_number = vp2.po_number
                       AND (TO_CHAR(VP2.PAYMENT_DATE,'Q') = '4')
                       AND ( TO_CHAR(VP2.PAYMENT_DATE,'YYYY') = '2009' ))
    AND (TO_CHAR(AV2.AWDATE,'Q') = '4')
    AND (to_CHAR(AV2.AWDATE,'YYYY') = '2009')
    GROUP BY TO_CHAR(NVL(AV2.TAX_ID,999999999),'000000000'),
      AV2.FIRM_NAME,
      AV2.BIDCLASS,
      AV2.AWAMT,
      AV2.SOL_MODE,
      AV2.CERT,
      AV2.ETHNICITY,
      AV2.PO_NUMBER_FORMAT Can someone provide a solution?
    Thank you,
    Robert

    Hi,
    You can take the parameters to the select so that you will be able to create conditions over them.
    Try to use this SQL instead of your and in the discoverer workbook create the conditions and parameters:
    SELECT TO_CHAR(NVL(AV.TAX_ID,999999999),'000000000') FEID,
    AV.FIRM_NAME VENDOR_NAME,
    AV.BIDCLASS CONTRACT_CODES,
    AV.AWAMT AWARD_AMOUNT,
    AV.SOL_MODE FORMAL_INFORMAL,
    AV.CERT BUSINESS_ENTITY,
    AV.ETHNICITY ETHNICTY,
    AV.PO_NUMBER_FORMAT CONTRACT,
    TO_CHAR(VP.PAYMENT_DATE,'YYYY') P_YEAR,
    TO_CHAR(VP.PAYMENT_DATE,'Q') P_QTR
    SUM(VP.INVOICE_AMOUNT) AMOUNT_PAID_$
    FROM CONFIRM.VSTATE_PAID_AWARD_VENDORS AV,
    CONFIRM.VSTATE_VENDOR_PAYMENTS VP
    WHERE ( ( AV.PO_NUMBER = VP.PO_NUMBER
    AND AV.VENDOR_ID = VP.VENDOR_ID ) )
    --AND (TO_CHAR(VP.PAYMENT_DATE,'Q') = '4')*
    --AND ( TO_CHAR(VP.PAYMENT_DATE,'YYYY') = '2009' )*
    GROUP BY TO_CHAR(NVL(AV.TAX_ID,999999999),'000000000'),
    AV.FIRM_NAME,
    AV.BIDCLASS,
    AV.AWAMT,
    AV.SOL_MODE,
    AV.CERT,
    AV.ETHNICITY,
    AV.PO_NUMBER_FORMAT ,
    TO_CHAR(VP.PAYMENT_DATE,'YYYY') P_YEAR,
    TO_CHAR(VP.PAYMENT_DATE,'Q') P_QTR
    union
    SELECT TO_CHAR(NVL(AV2.TAX_ID,999999999),'000000000') FEID,
    AV2.FIRM_NAME VENDOR_NAME,
    AV2.BIDCLASS CONTRACT_CODES,
    AV2.AWAMT AWARD_AMOUNT,
    AV2.SOL_MODE FORMAL_INFORMAL,
    AV2.CERT BUSINESS_ENTITY,
    AV2.ETHNICITY ETHNICTY,
    AV2.PO_NUMBER_FORMAT CONTRACT,
    TO_CHAR(VP.PAYMENT_DATE,'YYYY') P_YEAR,
    TO_CHAR(VP.PAYMENT_DATE,'Q') P_QTR
    0 AMOUNT_PAID_$
    FROM CONFIRM.VSTATE_PAID_AWARD_VENDORS AV2
    WHERE
    not exists (SELECT 'X'
    FROM CONFIRM.VSTATE_VENDOR_PAYMENTS VP2
    WHERE av2.po_number = vp2.po_number
    AND (TO_CHAR(VP2.PAYMENT_DATE,'Q') = TO_CHAR(VP.PAYMENT_DATE,'Q') )
    AND ( TO_CHAR(VP2.PAYMENT_DATE,'YYYY') = TO_CHAR(VP.PAYMENT_DATE,'YYYY') ))
    --AND (TO_CHAR(AV2.AWDATE,'Q') = '4')*
    --AND (to_CHAR(AV2.AWDATE,'YYYY') = '2009')*
    GROUP BY TO_CHAR(NVL(AV2.TAX_ID,999999999),'000000000'),
    AV2.FIRM_NAME,
    AV2.BIDCLASS,
    AV2.AWAMT,
    AV2.SOL_MODE,
    AV2.CERT,
    AV2.ETHNICITY,
    AV2.PO_NUMBER_FORMAT,
    TO_CHAR(VP.PAYMENT_DATE,'YYYY') P_YEAR,
    TO_CHAR(VP.PAYMENT_DATE,'Q') P_QTR
    Tamir

  • Order by clause with union

    Hi,
    I wanted to know if this is possible with order by clause and union. some how union messes up the ordering of the left part of the union statement.
    assume the data is as below in table tab1
    c1 c2
    4 1
    4 2
    5 1
    5 2
    7 1
    7 2
    8 3
    9 4
    the expected output must be for all c1 < 7 ordering must be on c1 and for all c1 > 7 ordering must be on c2.
    so the query i tried was
    select c1,c2 from tab1 where c1 <7 order by c1
    union
    select c1,c2 from tab1 where c1 >=7 order by c2
    it is giving the message 'Sql command not properly ended'.
    Suggestions are welcome

    SQL>
    SQL> With t As
      2  (
      3  Select 4 c1, 1 c2 From dual Union All
      4  Select 4 ,2       From dual Union All
      5  Select 5 ,1       From dual Union All
      6  Select 5 ,2       From dual Union All
      7  Select 7 ,1       From dual Union All
      8  Select 7 ,2       From dual Union All
      9  Select 8 ,3       From dual Union All
    10  Select 9 ,4       From dual
    11  ), t1 As (Select c1,c2 From t Where c1 <7 Order By c1)
    12  ,t2 As (select c1,c2 from t where c1 >=7 Order By c2)
    13  Select * From t1 Union All
    14  Select * From t2;
            C1         C2
             4          1
             4          2
             5          1
             5          2
             7          1
             7          2
             8          3
             9          4
    8 rows selected
    SQL> /*-- Union All
      2   select c1,c2 from t where c1 >=7
      3   Order By c1,c2;
      4  */
      5 
    with union clause order by must be placed in the end,,
    select c1 from dual union all
    select c2 from dual
    order by  c1

  • Report with Union

    Hello Gurus
    I have a report with UNION of 3 criteria and the result i am getting is one of the columns is a union of 3 columns like Age Group,Gender and Race and here the problem is i need to show them in order Race first, Age Group, Gender now it is showing in sorting order even though i cleared sort on all the columns 65+,75+,85+, Asain, Black,Female,Hispanic,Male instead i want the column to show me Asian, Black,Hispanic, 65+,75+,85+,Female, male also in that table i want to make the table line thicker after race category and Age Group
    Thanks
    RC

    user1146711 wrote:
    Hello Gurus
    I have a report with UNION of 3 criteria and the result i am getting is one of the columns is a union of 3 columns like Age Group,Gender and Race and here the problem is i need to show them in order Race first, Age Group, Gender now it is showing in sorting order even though i cleared sort on all the columns 65+,75+,85+, Asain, Black,Female,Hispanic,Male instead i want the column to show me Asian, Black,Hispanic, 65+,75+,85+,Female, male also in that table i want to make the table line thicker after race category and Age Group
    Thanks
    RCTry this:
    1) In each of your three separate queries, add another column.
    2) Create BINS according to how you want the final report be sorted: Asian = '1', Black = '2', etc...
    3) Sort on this column in the combined query.
    Since each query will have the proper number assigned to the group, the report will sort as you wish...

  • How to insert some strings in a query with the clausule 'in' in a procedure

    Hello fellows.
    Do you know how to insert some strings in a query with the clausule 'in' in a procedure (with a variable)?.
    I tell us my problem with an example:
    I want to use this select but introducing a variable instead the 'value1', 'value2':
    select * from table1 where field1 in ('value1', 'value2');
    if I declare the variable var1 as:
    TC_VAR1 CONSTANT VARCHAR2(50) := '''value1'',''value2'''
    and I use it as:
    select * from table1 where field1 in (TC_VAR1);
    It doesn't work because Oracle takes TC_VAR1 as an all string. I think Oracle takes it as this:
    select * from table1 where field1 in ('value1, value2');
    If I use the data type TABLE, I don't know how to use well:
    TYPE varchar2_table IS TABLE OF VARCHAR2(10);
    tb_var varchar2_table;
    select * from table1 where field1 in (tb_var);->It returns an error and there ins't an method that it returns all values.
    A curious case is that if I have a sql script (for example script1.sh) with this query:
    select * fromt able1 where field1 in (&1)
    and I invoke this script as
    script1.sh "'''value1'',''value2'''". It works
    Can anybody helps me?.
    Thanks.

    Thanks to all. Really.
    First, sorry for my English. It's horrible. Thank to all for understand it (my English).
    I've resolved my problem with these links:
    ORA-22905: cannot access rows from a non-nested table item
    and
    http://stackoverflow.com/questions/896356/oracle-error-ora-22905-cannot-access-rows-from-a-non-nested-table-item
    At last, I have used someting like this:
    DECLARE
    number_table_tmp NUM_ARRAY:=NUM_ARRAY(410673, 414303, 414454, 413977, 414042, 414115, 413972, 414104, 414062);
    BEGIN
    FOR c1 IN (SELECT par_id, 1 acc_vdo_id FROM SIG_VIS_CAM
    WHERE par_id IN (SELECT * FROM TABLE(number_table_tmp))
    UNION ALL
    SELECT par_id, 2 acc_vdo_id FROM SIG_ACCAO a
    WHERE par_id IN (SELECT * FROM TABLE(number_table_tmp))) LOOP
    NULL;
    END LOOP;
    END;
    but first I had to create the type NUM_ARRAY like this:
    CREATE TYPE subjectid_tab AS TABLE OF NUMBER INDEX BY binary_integer;
    THANK YOU, GUYS. YOU ARE THE BESTS. ;-)
    Edited by: user12249099 on 08-sep-2011 7:37
    Edited by: user12249099 on 08-sep-2011 7:37

  • Oracle hangs on query with multiple joins

    To test the sotftware from the LMS-vendor that are out partners we use virtual machines where we install Windows 2003 servers and Oracle XE
    While we were testing the software I've found that a particular query with multiple unions causes the CPU of the virtual machine totally claimed by oracle
    The query causes oracle to hang.
    I've found that the subcomponents of the query all return 0 rows (and response all immediately) combined into a query with at least 2 unions the query hangs the system
    I'm not familiar with with Database Management at all
    I've read something about SGA and PGA that could be the issue and tried to increase the target for both but it doesn't seem to do anything
    Some characterics
    Target Current
    Maximum System Global Area (SGA) Size: 380 MB 380 MB
    Program Global Area (PGA) Aggregate Target: 360 MB 38 MB
    Current Configuration: (SGA + PGA): 740 MB 418 MB
    Tablespaces Percent Used Allocated (MB) Used (MB) Datafiles
    SYSAUX 98.21% 460.00 451.75 1
    SYSTEM 73.09% 510.00 372.75 1
    DATA 99.13% 440.00 436.19 1
    UNDO 6.48% 160.00 10.38 1
    USERS 1.63% 100.00 1.63 1
    sort_area_size 65536
    shared_pool_reserved_size 5452595 TRUE size in bytes of reserved area of shared pool
    shared_pool_size 0 TRUE size in bytes of shared pool
    What other parameters are important? How could I get a good picture to see what's really going on?
    Some pointers, help would be appreciated.
    Regards,
    Remco

    Below is the base-query that is causing the problems
    SELECT /* Public - Internal learner */ r.id reg_id, i.id order_item_id, o.id order_id, o.order_no order_number, e.id person_id, format_name(e.fname , e.lname , @@005) learner_name , c.id company_id, c.name2 , ent.id entid, ctype.id ctypeid, ctype.name , i.status , items.description item_desc, substr ( i.flags , 1 , 1 ) is_conf , r.status status1, rs.description description1 , r.wlist_priority , r.reg_no , r.flags , o.status status2, o.split, null learnerViewOnly, null statusViewOnly, i.approved_status, decode(substr(r.flags,2,1),'1','true','false') isWalkIn, oa.id offering_action_id, oa.status profile_status ,c.name2 org_name ,ctype.name audience_sub_type ,items.description description ,o.order_no order_no ,orderList.description order_status ,approvalList.description approval_status ,e.fname learner_first_name ,e.lname learner_last_name FROM tpt_registration r INNER JOIN tpt_oe_order_items i ON i.reg_id = r.id INNER JOIN tpt_oe_order o ON i.order_id = o.id INNER JOIN cmt_person e ON r.student_id = e.id INNER JOIN tpt_company c ON e.company_id = c.id INNER JOIN tpt_offering_action oa on r.offering_action_id = oa.id LEFT OUTER JOIN tpt_roster_template_entry ent ON r.ros_temp_ent_id = ent.id LEFT OUTER JOIN tpt_customer_type ctype ON ent.customer_type_id = ctype.id INNER JOIN fgt_ext_sys_list_of_val rs ON to_char ( r.status ) = rs.name INNER JOIN fgt_ext_sys_list_of_val items ON to_char ( i.status ) = items.name INNER JOIN fgt_ext_sys_list_of_val orderList ON to_char ( o.status ) = orderList.name INNER JOIN fgt_ext_sys_list_of_val approvalList ON to_char(i.approved_status) = approvalList.name WHERE e.type = 100 AND r.class_id = @@001 AND r.status = nvl ( to_number(@@002) , r.status ) AND rs.locale_id = @@005 AND rs.list_id = 'sysli000000000000100' AND items.locale_id = @@005 AND items.list_id = 'sysli000000000000131' AND orderList.list_id = 'sysli000000000000129' AND orderList.locale_id = @@005 AND approvalList.list_id = 'sysli000000000000165' AND approvalList.locale_id = @@005 AND ((@@003 is null) or (@@003 is not null and r.student_id = @@003))
    UNION
    SELECT /* Public - External learner */ r.id reg_id, i.id order_item_id, o.id order_id, o.order_no , e.id person_id, format_name(e.fname , e.lname , @@005) , c.id company_id, c.name2 , ent.id entid, ctype.id ctypeid, ctype.name , i.status , items.description , substr ( i.flags , 1 , 1 ) is_conf , r.status status1, rs.description description1, r.wlist_priority , r.reg_no , r.flags , o.status status2, o.split, null learnerViewOnly, null statusViewOnly, i.approved_status, decode(substr(r.flags,2,1),'1','true','false') isWalkIn, oa.id offering_action_id, oa.status profile_status ,c.name2 org_name ,ctype.name audience_sub_type ,items.description description ,o.order_no order_no ,orderList.description order_status ,approvalList.description approval_status ,e.fname learner_first_name ,e.lname learner_last_name FROM tpt_registration r INNER JOIN tpt_oe_order_items i ON i.reg_id = r.id INNER JOIN tpt_oe_order o ON i.order_id = o.id INNER JOIN cmt_person e ON r.student_id = e.id INNER JOIN tpt_offering_action oa on r.offering_action_id = oa.id LEFT OUTER JOIN tpt_company c ON e.company_id = c.id LEFT OUTER JOIN tpt_roster_template_entry ent ON r.ros_temp_ent_id = ent.id LEFT OUTER JOIN tpt_customer_type ctype ON ent.customer_type_id = ctype.id INNER JOIN fgt_ext_sys_list_of_val rs ON to_char ( r.status ) = rs.name INNER JOIN fgt_ext_sys_list_of_val items ON to_char ( i.status ) = items.name INNER JOIN fgt_ext_sys_list_of_val orderList ON to_char ( o.status ) = orderList.name INNER JOIN fgt_ext_sys_list_of_val approvalList ON to_char(i.approved_status) = approvalList.name WHERE e.type = 200 AND r.class_id = @@001 AND r.status = nvl ( to_number(@@002) , r.status ) AND rs.locale_id = @@005 AND rs.list_id = 'sysli000000000000100' AND items.locale_id = @@005 AND items.list_id = 'sysli000000000000131' AND orderList.list_id = 'sysli000000000000129' AND orderList.locale_id = @@005 AND approvalList.list_id = 'sysli000000000000165' AND approvalList.locale_id = @@005 AND ((@@003 is null) or (@@003 is not null and r.student_id = @@003))
    UNION
    SELECT /* Public - Unassigned learner */ r.id reg_id, i.id order_item_id, o.id order_id, o.order_no , null person_id, null , null company_id, null , ent.id entidd, ctype.id ctypeid, ctype.name , i.status , items.description , substr ( i.flags , 1 , 1 ) is_conf , r.status status1, rs.description description1, r.wlist_priority , r.reg_no , r.flags , o.status status2, o.split, null learnerViewOnly, null statusViewOnly, i.approved_status, decode(substr(r.flags,2,1),'1','true','false') isWalkIn, oa.id offering_action_id, oa.status profile_status ,'' org_name ,ctype.name audience_sub_type ,items.description description ,o.order_no order_no ,orderList.description order_status ,approvalList.description approval_status ,'' learner_first_name ,'' learner_last_name FROM tpt_registration r INNER JOIN tpt_oe_order_items i ON i.reg_id = r.id INNER JOIN tpt_oe_order o ON i.order_id = o.id INNER JOIN tpt_offering_action oa on oa.id = r.offering_action_id LEFT OUTER JOIN tpt_roster_template_entry ent ON r.ros_temp_ent_id = ent.id LEFT OUTER JOIN tpt_customer_type ctype ON ent.customer_type_id = ctype.id INNER JOIN fgt_ext_sys_list_of_val rs ON to_char ( r.status ) = rs.name INNER JOIN fgt_ext_sys_list_of_val items ON to_char ( i.status ) = items.name INNER JOIN fgt_ext_sys_list_of_val orderList ON to_char ( o.status ) = orderList.name INNER JOIN fgt_ext_sys_list_of_val approvalList ON to_char(i.approved_status) = approvalList.name WHERE r.class_id = @@001 AND r.status = nvl ( to_number(@@002) , r.status ) AND r.student_id is null AND rs.locale_id = @@005 AND rs.list_id = 'sysli000000000000100' AND items.locale_id = @@005 AND items.list_id = 'sysli000000000000131' AND orderList.list_id = 'sysli000000000000129' AND orderList.locale_id = @@005 AND approvalList.list_id = 'sysli000000000000165' AND approvalList.locale_id = @@005 AND @@003 is null
    UNION
    SELECT /* Private - Internal learner */ r.id reg_id, i.id order_item_id, o.id order_id, o.order_no , e.id person_id, format_name(e.fname , e.lname , @@005) , c.id company_id, c.name2 , ent.id entid, ctype.id ctypeid, ctype.name , i.status , items.description , substr ( i.flags , 1 , 1 ) is_conf , r.status status1, rs.description description1 , r.wlist_priority , r.reg_no , r.flags , o.status status2, o.split, null learnerViewOnly, null statusViewOnly, i.approved_status, decode(substr(r.flags,2,1),'1','true','false') isWalkIn, oa.id offering_action_id, oa.status profile_status ,c.name2 org_name ,ctype.name audience_sub_type ,items.description description ,o.order_no order_no ,orderList.description order_status ,approvalList.description approval_status ,e.fname learner_first_name ,e.lname learner_last_name FROM tpt_registration r INNER JOIN let_pvt_offering_request pvt_offreq ON pvt_offreq.class_id = r.class_id INNER JOIN tpt_offering_action oa on oa.id = r.offering_action_id LEFT OUTER JOIN tpt_oe_order_items i ON i.part_id = pvt_offreq.id LEFT OUTER JOIN tpt_oe_order o ON i.order_id = o.id INNER JOIN cmt_person e ON r.student_id = e.id LEFT OUTER JOIN tpt_company c ON e.company_id = c.id LEFT OUTER JOIN tpt_roster_template_entry ent ON r.ros_temp_ent_id = ent.id LEFT OUTER JOIN tpt_customer_type ctype ON ent.customer_type_id = ctype.id INNER JOIN fgt_ext_sys_list_of_val rs ON to_char ( r.status ) = rs.name LEFT OUTER JOIN fgt_ext_sys_list_of_val items ON to_char ( i.status ) = items.name AND items.locale_id = @@005 AND items.list_id = 'sysli000000000000131' LEFT OUTER JOIN fgt_ext_sys_list_of_val orderList ON to_char ( o.status ) = orderList.name AND orderList.list_id = 'sysli000000000000129' AND orderList.locale_id = @@005 LEFT OUTER JOIN fgt_ext_sys_list_of_val approvalList ON to_char(i.approved_status) = approvalList.name AND approvalList.list_id = 'sysli000000000000165' AND approvalList.locale_id = @@005 WHERE e.type = 100 AND r.class_id = @@001 AND r.status = nvl ( to_number(@@002) , r.status ) AND rs.locale_id = @@005 AND rs.list_id = 'sysli000000000000100' AND ((@@003 is null) or (@@003 is not null and r.student_id = @@003))
    UNION
    SELECT /* Private - External learner */ r.id reg_id, i.id order_item_id, o.id order_id, o.order_no , e.id person_id, format_name(e.fname , e.lname , @@005) , c.id company_id, c.name2 , ent.id entid, ctype.id ctypeid, ctype.name , i.status , items.description , substr ( i.flags , 1 , 1 ) is_conf , r.status status1, rs.description description1 , r.wlist_priority , r.reg_no , r.flags , o.status status2, o.split, null learnerViewOnly, null statusViewOnly, i.approved_status, decode(substr(r.flags,2,1),'1','true','false') isWalkIn, oa.id offering_action_id, oa.status profile_status ,c.name2 org_name ,ctype.name audience_sub_type ,items.description description ,o.order_no order_no ,orderList.description order_status ,approvalList.description approval_status ,e.fname learner_first_name ,e.lname learner_last_name FROM tpt_registration r INNER JOIN let_pvt_offering_request pvt_offreq ON pvt_offreq.class_id = r.class_id INNER JOIN tpt_offering_action oa on r.offering_action_id = oa.id LEFT OUTER JOIN tpt_oe_order_items i ON i.part_id = pvt_offreq.id LEFT OUTER JOIN tpt_oe_order o ON i.order_id = o.id INNER JOIN cmt_person e ON r.student_id = e.id LEFT OUTER JOIN tpt_company c ON e.company_id = c.id LEFT OUTER JOIN tpt_roster_template_entry ent ON r.ros_temp_ent_id = ent.id LEFT OUTER JOIN tpt_customer_type ctype ON ent.customer_type_id = ctype.id INNER JOIN fgt_ext_sys_list_of_val rs ON to_char ( r.status ) = rs.name LEFT OUTER JOIN fgt_ext_sys_list_of_val items ON to_char ( i.status ) = items.name AND items.locale_id = @@005 AND items.list_id = 'sysli000000000000131' LEFT OUTER JOIN fgt_ext_sys_list_of_val orderList ON to_char ( o.status ) = orderList.name AND orderList.locale_id = @@005 AND orderList.list_id = 'sysli000000000000129' LEFT OUTER JOIN fgt_ext_sys_list_of_val approvalList ON to_char(i.approved_status) = approvalList.name AND approvalList.locale_id = @@005 AND approvalList.list_id = 'sysli000000000000165' WHERE e.type = 200 AND r.class_id = @@001 AND r.status = nvl ( to_number(@@002) , r.status ) AND rs.locale_id = @@005 AND rs.list_id = 'sysli000000000000100' AND ((@@003 is null) or (@@003 is not null and r.student_id = @@003)) ORDER BY 34,35,28,29,31,30,33,32

  • Select query with pivot

    Hi All,
    I have written a query as belows: but I dont get the output as desired.
    This is a with query -  I need the query output in a single record so I have pivoted the first query spl. The second set of query stk gives me the stock.
    My requirement is whnever there is a not matching mmg_id of 1st query with mmg5_id of 2nd query it should give the second record as follows
    LOC_LOC_ID      MMG_ID     TP_TP_ID      'S10'       'O3'            'O7'            'O8'       CN_STK_QTY      CN_STK_VAL
    1097                  1610           20132123       207.5      62.08       7504           25.06      603                       93027.5
    1097                  0024          20132123                                                                    -1                        -15.99
    {code}
    Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.2.0
    Connected as sizsupport
    SQL>
    SQL> WITH spl AS
      2  (
      3  SELECT * FROM
      4  (SELECT '1097' loc_loc_id, '1610' mmg_id, '20132123' tp_tp_id, 'S10' measure_name,'207.5' val FROM dual UNION ALL
      5  SELECT '1097' , '1610' , '20132123' , 'O3' measure_name, '62.08'  FROM dual UNION ALL
      6  SELECT '1097' , '1610' , '20132123' , 'O7' measure_name, '7504'  FROM dual UNION ALL
      7  SELECT '1097' , '1610' , '20132123' , 'O8' measure_name, '25.06'  FROM dual )
      8  pivot ( sum(val) FOR measure_name IN
      9  ('S10','O3','O7','O8'))
    10  ),
    11  stk AS
    12  (
    13  SELECT '1097' loc_loc_id, '0024' mmg5_id, '20132123' tp_tp_id, '-1' cn_stk_qty, '-15.99' cn_stk_val FROM dual
    14  UNION ALL
    15  SELECT '1097' loc_loc_id, '1610' mmg5_id, '20132123' tp_tp_id, '603' cn_stk_qty, '93027.5' cn_stk_val FROM dual
    16  )
    17  SELECT spl.*,stk.cn_stk_qty,stk.cn_stk_val
    18  FROM spl  RIGHT OUTER JOIN stk
    19  ON spl.loc_loc_id = stk.loc_loc_id
    20  AND spl.tp_tp_id  = stk.tp_tp_id
    21  AND spl.mmg_id    = stk.mmg5_id
    22  ;
    LOC_LOC_ID MMG_ID TP_TP_ID      'S10'       'O3'       'O7'       'O8' CN_STK_QTY CN_STK_VAL
    1097       1610   20132123      207.5      62.08       7504      25.06 603        93027.5
                                                                           -1         -15.99
    SQL>
    {code}

    Hi,
    When there is no match between stk and spl, then all columns from spl will be NULL, but the values from stk will be available.  Take the columns you need to display from stk, not from spl.
    WITH  spl  AS
        SELECT  *
        FROM    raw_data
        PIVOT   (   SUM (val)
                FOR measure_name IN ( 'S10' AS s10
                                    , 'O3' AS p_03
                                    , 'O7' AS p_07
                                    , 'O8' AS p_08
    SELECT  stk.loc_loc_id
    ,       stk.mmg5_id
    ,       stk.tp_tp_id
    ,       spl.s10
    ,       spl.p_03
    ,       spl.p_07
    ,       spl.p_08
    ,       stk.cn_stk_qty
    ,       stk.cn_stk_val
    FROM             stk
    LEFT OUTER JOIN  spl  ON   spl.loc_loc_id = stk.loc_loc_id
                          AND  spl.tp_tp_id   = stk.tp_tp_id
                          AND  spl.mmg_id     = stk.mmg5_id
    I gave the pivoted columns standard names, like s10 and p_03.  You don't have to do that; you can use "'S10'" and "'P_03'" if you prefer.

  • Materalized view with union all and fast referesh

    I have a one view which is very slow. in this view we are joining many tables and many union all queries.
    now I am planing to make materalized view
    Tell me how i will created view with fast refresh with union all query.
    Pls help its urgent..
    Thanks
    Reena

    Refer to the Replication Manual for the create syntax and exceptions.

  • How to create VO with multiple dynamic where clauses on select with UNION?

    I am trying to implement the View Object for the UNION query that looks like this:
         select a,b,c...
              from t1,t2,...
              where dynamic_where_clause1     
         union all
         select a,b,c,...
              from t11,t12, ...
              where dynamic_where_clause2
    There are up to 60 input parameters that are used to generate dynamic where clauses. They are actually created by calling PL SQL function.
    So far, I was not able to assign both where clauses to the view object. Is there a workable solution for this problem, besides resorting to programmatic View Object?
    I understand that recommended way with UNIONs is to wrap both queries into a parent select:
    select * from (
         select a,b,c...
              from t1,t2,...
              where ... -- table relationship joints
         union all
         select a,b,c,...
              from t11,t12, ...
              where ... -- table relationship joints
    ) QRSLT
         where dynamic_where_clause
    Unfortunately this approach doesn't work here, since individual selects are producing unmanageable amount of data and resulting query takes forever to complete.

    I afraid I would not have any real benefits from using VO if I replace the entire query with every request. Actually, the performance may suffer.
    I solved the problem by creating a POJO Data Control and invoking the custom select query from java. Not sure if it is the best approach to the problem, but implementation time is limited and it works.
    Actually, this is not the first time I see the need to implement VO with complicated SQL like select with unions and dynamic pieces. It would be nice to find a solution and not resort to workarounds.
    Edited by: viksicom on Aug 2, 2012 8:48 AM

Maybe you are looking for

  • Invalid Oracle URL specified: OracleDataSource.makeURL in Arquillian ITest

    I asked this on Stackoverflow but haven't received any feedback.  oracle11g - Invalid Oracle URL specified: OracleDataSource.makeURL in Arquillian ITest - Stack Overflow   I'm hoping for a response here. I work hard to resolve any and every error I g

  • WHT Code with Tax Slabs

    Dear All, I need to configure a tax code in the system as per following three conditions: No..Condition...................................... .Rate 1...Gross Amount (GA) <= 150,000........0% 2...GA > 150,000 and GA < 400,000.......5% 3...GA > 400,000

  • HT1918 Bad instructions on changing payment method? Or software problem?

    I'm using an iPad and getting a message that there's a problem with a past purchase. When I click that "payment information" button, I just get the same message over and over- the only options I can select are "cancel" or "done", there's no option th

  • SD Billing Document Number Ranges exhausted

    hi, we have used separate number range for billing document for each plant. further within each plant uses five ranges - separate for Invoice, Sales Return, Credit Note, Debit Note & cancellation. we have used all possible combinations in FBN1. now t

  • Cant sync iPod require file is not found

    Has anyone received this error message? If so, anyway to correct it?