Poblem with union/union all giving ORA-22950

I use a select statement with the xmlelement to create an xml file.

There the various unions end in 5, but I always bring a line through.

I do like that as then I can use this as a cursor in my plsql without having to create lots of cursors when I only need to use a.
That is to say.
SELECT  xmlelement("QUOTATION",xmlagg(xmlelement(Quotation,
                      xmlelement(CUSTOMERNAME, customername),
                      xmlelement(QUOTE,quoteid )))xmlfile
From cust, quote
where custid=quotecustid
and ptype = 'Q'
UNION
SELECT  xmlelement("SALES",xmlagg(xmlelement(SALES,
                      xmlelement(CUSTOMERNAME, customername),
                      xmlelement(QUOTE,salesid )))xmlfile
From cust, sales
where custid=salescustid
and ptype = 'S'
Then, I use a cursor to execute it in a ftp file folder I selected passing ptype as a parameter.

If I use the union I get ORA-22950 - I have seen other people use UNION all around it.
If I use union all I'll get a value for the SALE of any line that is

Any ideas how I can get around this as do not want to continue to create cursors

The use of different sliders is always the best approach IMO.
All things considered, it should be more effective than that appearing more "smart" one.

And there is no need to be explicit cursors, it could just be SELECT INTOs wrapped in a PL/SQL CASE statement, with the appropriate exception handler (if necessary).

Anyway, if you want to stick with the single-cursor method, there are different ways to do this:

(1) adding a column selector:

SELECT xmlfile
FROM (
  SELECT 'Q' as selector
       , xmlelement("QUOTATION",xmlagg(xmlelement(Quotation,
                  xmlelement(CUSTOMERNAME, customername),
                  xmlelement(QUOTE,quoteid )))xmlfile
  From cust, quote
  where custid=quotecustid
  and ptype = :1
  UNION ALL
  SELECT 'S'
       , xmlelement("SALES",xmlagg(xmlelement(SALES,
                  xmlelement(CUSTOMERNAME, customername),
                  xmlelement(QUOTE,salesid )))xmlfile
  From cust, sales
  where custid=salescustid
  and ptype = :1
)
WHERE selector = :1
;

(2) adding a GROUP BY clause, so that XMLAgg returns nothing if no row is selected:

SELECT xmlelement("QUOTATION",xmlagg(xmlelement(Quotation,
                xmlelement(CUSTOMERNAME, customername),
                xmlelement(QUOTE,quoteid )))xmlfile
From cust, quote
where custid=quotecustid
and ptype = :1
GROUP BY null
UNION ALL
SELECT xmlelement("SALES",xmlagg(xmlelement(SALES,
                xmlelement(CUSTOMERNAME, customername),
                xmlelement(QUOTE,salesid )))xmlfile
From cust, sales
where custid=salescustid
and ptype = :1
GROUP BY null
;

(3) subqueries:

SELECT case :1
        when 'Q' then ( SELECT ... )
        when 'S' then ( SELECT ... )
      end as xmlfile
FROM dual
;

Tags: Database

Similar Questions

  • with the clause with union all?

    Hello

    I tried to use 'by' article with union all. .but it gives me error...
       with t1 as 
            (select '1'col1 from dual union all
             select '2' col2 from dual)select * from t1
             union 
            with t2 as 
            (select '3' col1 from dual union all
             select '4' col1 from dual )
              select * from t2
                
    What harm am I doing here? Thank you very much!!

    Use the WITH clause, once for you all the subqueries, and then add the main request;

    with t1 as
     (select '1' col1
      from dual
      union all
      select '2' col2
      from dual),
    t2 as
     (select '3' col1
      from dual
      union all
      select '4' col1
      from dual)
    select *
    from t1
    union
    select *
    from t2;
    
  • UNION and UNION ALL giving multiple result sets even if INTERSECT does not lines.

    Hello

    I have a set of two queries. I used the UNION to join the result set. When I used UNION ALL, I get a different result set.

    BT when I use INTERSECT, I m not getting all the lines.

    SO, which I guess is, as operation INTERSECT isn't Govind all the lines, then the UNION and UNION ALL of the result sets must be simliar.

    But I m getting different result sets.

    Please guide me.

    Thank you.

    Hello

    UNION returns separate lines; UNION ALL returns all rows that produce queries.

    INTERSECT has nothing to do with it.  You can have the lines in a single query.  For example

    Job SELECTION

    FROM scott.emp

    UNION - ALL THE

    SELECT 'FUBAR '.

    DOUBLE;

    In this example, there is no overlap between the 2 queries (INTERSECT would have 0 rows).

    UNION produces 6 lines, because the query at the top of the page produces 5 distinct lines (of 14 total ranks) and the background query 1.

    UNION ALL product lines 15: 14 of the request from top and 1 of the request from the lower part.

    I hope that answers your question.

    If not, post a test script (if necessary) and complete, including some UNION, UNION ALL queries INTERSECT.  Post of the CREATE TABLE and INSERT statements for all tables using those queries (with the exception of the commonly available rtables, such as those of the scott schema) and a specific question, such as "the UNION query all product...» I expect the UNION query to produce... because... but instead, it produces... Why is this?  It seems contractict... manual which says that... ».

  • Problem with Union All

    Hello Experts,

    I'm still on the learning curve in PL/SQL and I have 2 different. I need to create a view that will merge these 2 tables.
    create view eft_view as
    select 
      BILL_TO_RETAILER      ,
      RETAILER_NAME         ,
      NAME_ON_BANK_ACCOUNT  ,
      BANK_ABA              ,
      BANK_ACCT             ,
      ON_LINE_AMOUNT        ,
      INSTANT_AMOUNT        ,
      TOTAL_AMOUNT          ,
      SOURCE                ,
      INSERTED_DATE         ,
      CDC                   from weekly_eft_temp union all
    select 
      BILL_TO_RETAILER  ,
      RETAILER_NAME     ,
      STATUS            ,
      ON_LINE_AMOUNT    ,
      INSTANT_AMOUNT    ,
      NON_SWEPT_AMT     ,
      TTYPE             ,
      EXCEPTION_REASON   from weekly_bill_expt_temp;
    I used the union of all, but it doesn't seem to work. I don't know where it's track. I searched on the net but his send me examples for 2 tables tat are similar.

    Any suggestions please

    You must replicate the columns that do not exist fill them with NULL values.

    Perhaps;

    create view eft_view as
      select bill_to_retailer,
             retailer_name,
             name_on_bank_account,
             bank_aba,
             bank_acct,
             null status,
             on_line_amount,
             instant_amount,
             total_amount,
             source,
             inserted_date,
             cdc,
             cast (null as number) non_swept_amt,
             null ttype,
             null exception_reason
        from weekly_eft_temp
      union all
      select bill_to_retailer,
             retailer_name,
             null name_on_bank_account,
             null bank_aba,
             null bank_acct,
             status,
             on_line_amount,
             instant_amount,
             null total_amount,
             null source,
             null inserted_date,
             nul cdc,
             non_swept_amt,
             ttype,
             exception_reason
        from weekly_bill_expt_temp;
    
  • show all columns with union all

    Hi all
    select m_id, total, cat_id
    from scott.maintenance
    where prod_id = 1
    union all
    select m_id, total, prod_id
    from scott.maintenance
    where prod_id = 4
    How can I view all (and not common) columns of the two tables when I use union all like
    m_Id, total, cat_id, prod_id

    Thank you
    Johnny

    Hi, Johnny,.

    user9542267 wrote:
    Hi all

    select m_id, total, cat_id
    from scott.maintenance
    where prod_id = 1
    union all
    select m_id, total, prod_id
    from scott.maintenance
    where prod_id = 4
    

    How can I view all (and not common) columns of the two tables when I use union all like
    m_Id, total, cat_id, prod_id

    Thank you
    Johnny

    There is no need to make a trade UNION for this:

    SELECT  *
    FROM    scott.maintenance
    WHERE   prod_id IN (1, 4)
    ;
    

    If you really need to do a UNION and basic tables were the same or had the same number and types of columns, then you can use SELECT * in the two branches of the UNION:

    SELECT  *
    FROM    scott.maintenance
        --
    UNION ALL
        --
    SELECT  *
    FROM    foo.maintenance
    ;
    

    If the columns have different names in the two tables, the names of the first branch of the UNION will be used in the output.

    In all cases, you can add literal NULL values to the SELECT one or more branches of the UNION clause:

    SELECT  ename     AS name
    ,       hiredate
    ,       deptno
    FROM    scott.emp
        --
    UNION ALL
        --
    SELECT  dname     AS name        -- alias not necessary, but helpful
    ,       NULL      AS hiredate    -- alias not necessary, but helpful
    ,       deptno
    FROM    scott.dept
    ;
    

    Depending on your tables and your requirements of yor, you might be able to use SELECT * in a branch of the UNION.

    Published by: Frank Kulash, February 15, 2011 12:29

  • Adding columns with union all?

    --------------------------------------------------------------------------------------------------------
    SELECT BAGLANTIDATE, CABONE
    FROM SEDEF_BILGI
    WHERE CABONE = '1' AND
    BAGLANTIDATE BETWEEN TO_DATE('10-JUN-2009','DD,MON-YYYY')
    AND TO_DATE('11-JUN-2009','DD-MON-YYYY')
    
    UNION ALL
    
    SELECT KAYITDATE, ABONE
    FROM SEDEF_ISEMRI
    WHERE ABONE = '1' AND
    KAYITDATE BETWEEN TO_DATE('10-JUN-2009','DD,MON-YYYY')
    AND TO_DATE('11-JUN-2009','DD-MON-YYYY')
    ------------------------------------------------------------------------------------------------------------
    
    BAGLANTITARIH        ABONE
    ------------------------      ----------------
    10-JUN-09 03.01.52.546000000 AM 1          
    10-JUN-09 05.02.06.453000000 PM 1          
    10-JUN-09 01.01.20.421000000 PM 1          
    10-JUN-09 07.01.42.062000000 AM 1          
    10-JUN-09 09.44.59.515000000 AM 1          
    10-JUN-09 09.58.07.843000000 AM 1          
    10-JUN-09 09.01.50.546000000 PM 1    
    10-JUN-09 09.52.57.796000000 AM 1
    10-JUN-09 02.13.32.343000000 PM 1
    
    9 rows selected
    How can I add a column in the tables on that?

    Published by: niennamiriel on August 18, 2009 12:12 AM

    Yes it is possible.

    SELECT BAGLANTIDATE, CABONE, MSISDN
      FROM SEDEF_BILGI
      ...
    UNION ALL
    SELECT KAYITDATE, ABONE, null
      FROM SEDEF_ISEMRI
    
  • Relating to the creation of a graph, get an error with a statement "Union All".

    Hello

    I have some data in the table with the following fields;

    Product, Jul-08 Aug-08, Sep - 08 etc. to Jan - 10.

    The PRODUCT field is text showing many product names

    The month of Jul-08 & others have numbers by product that must be aggregated for this month & plotted on the graph of the line.

    My SQL to create the first point on the chart is as below;

    Select the link null, label, SUM(JUL-08) "FFF".
    of "SCHEMANAME." "' TABLENAME '.
    WHERE PRODUCT = "FFF".
    PRODUCT GROUP
    ORDER BY PRODUCT

    It works fine until I want to add the second point of this graphic line by using a "union all" join as follows:

    Select the link null, label, SUM (JUL_08) "FFF".
    of "SCHEMANAME." "' TABLENAME '.
    WHERE PRODUCT = "FFF".

    UNION ALL

    Select the link null, label, SUM (AUG_08) "FFF".
    of "SCHEMANAME." "' TABLENAME '.
    WHERE PRODUCT = "FFF".

    I can't work on how I can join the other months on the line graph of a series.

    The error is as follows;

    --------------------------------------

    1 error has occurred
    Failed to parse the SQL query:
    Click link null, label, SUM (OCT_09) 'Non-communicable diseases - STD' of 'BI_A_DATA '. "" CDW_VS_NCDS_CALLS "WHERE PRODUCT ="MNT - STD' UNION ALL select link null, PRODUCT, SUM (NOV_09) "Non-communicable diseases - LOCAL" label of "BI_A_DATA". "" CDW_VS_NCDS_CALLS "WHERE PRODUCT ="MNT - LOCAL.

    ORA-00937: not a single group group function

    Some queries can be run when you run your application, if your query is syntactically correct, you can save your query without validation (see options below the source of the query).

    ----------------------------------------

    If anyone can help?

    I want a continuous linear graph showing monthly July 2008, Aug-08 Sep - 08 etc. to Jan - 10 for the same product.

    I will then add other series for other products, thanks

    OK a graphic line will only allow you to draw 1 variable (in this case period). Given that it is only for a single product, so here should be the final sql:

    SELECT null link, PERIOD label, SUM(ABC)
    FROM
    (SELECT 'JUL_08' PERIOD,SUM(JUL_08)ABC
    FROM BI_A_DATA.APEX_TEST
    WHERE PRODUCT = 'ABC'
    
    UNION ALL
    
    SELECT 'AUG_08' PERIOD,SUM(AUG_08)ABC
    FROM BI_A_DATA.APEX_TEST
    WHERE PRODUCT = 'ABC'
    
    UNION ALL
    
    SELECT 'SEP_08' PERIOD,SUM(SEP_08)ABC
    FROM BI_A_DATA.APEX_TEST
    WHERE PRODUCT = 'ABC')
    
    GROUP BY PERIOD
    
  • Union all with addistional field

    Hello

    Maybe this is a stupid question.

    I have two tables with 22 columns.

    Another table with 23 columns

    I want to combine these two tables using union all.

    In the first picture, I want back "NA" as the last field.

    In the second table, last column is varchar2.

    Instead of every column one by one and if I use select * in two queries and add "NA" to one?

    I want the union two queries can order and use it with the clause to retrieve values based on conditions somany.

    So I have to specify the names of column three times.

    I use oracle 10g

    Hello Krishna,

    Try

    select t1.*, 'NA' as  from table1 t1
    union
    select t2.* from table2 t2;
    

    concerning

    Kay

  • Please help me with the Alternative of queries to replace the UNION ALL for two queries

    Hi all

    I have the query to retrieve assets employees salary count and in so far as below:

    Select ename, emp_no, sum (sal_till_2010), sum (sal_till_2014) of

    (select emp_no, ename, salary as sal_till_2010, 0 as sal_till_2014 of employee e1

    where effective_date < = 1 January 2010 ' and not exists (select 1 from e2 employee_deletion where e2.emp_no = e1.emp_no and e2.deletion_date < = January 1, 2010 "")

    UNION ALL

    Select ename, emp_no, 0 as sal_till_2010, salary as employee e1 sal_till_2014 - here is a dummy 0 salary until 2010 for the union of all the

    where effective_date < = 1 January 2014 "and not exists (select 1 from e2 employee_deletion where e2.emp_no = e1.emp_no and e2.deletion_date < = 1 January 2014") "

    Group of emp_no, ename;

    In this query, I get the total salary until 2010 and until 2014 in the employee table, dates are dynamically passed to the procedure, and this can change.

    But assume the date above and let me know the alternative of queries to improve performance because I use Union ALL and read the same table twice in the above query.

    Advice me with request to read the table once to fetch the same data as the above query.

    Thanks in advance.


    Hello

    Thanks for the display of the data of the sample; It's very useful!

    I think OP wants something like this:

    WITH cutoff_dates AS

    (

    SELECT TO_DATE (January 1, 2010 ', ' DD/MM/YYYY') AS cutoff_date, 2010 UNDER the label OF dual UNION ALL

    SELECT TO_DATE (1 January 2014 ', "DD/MM/YYYY"), double 2014

    )

    SELECT e.emp_no, e.ename

    , NVL (SUM (CASE WHEN c.label = 2010 THEN e.salary END), 0) AS sal_till_2010

    , NVL (SUM (CASE WHEN c.label = 2014 THEN e.salary END), 0) AS sal_till_2014

    E employee

    JOIN cutoff_dates c ON e.effective_date<=>

    WHERE DOES NOT EXIST)

    SELECT 1

    Of employee_deletion ed

    WHERE ed.emp_no = e.emp_no

    AND ed.deletion_date<=>

    )

    E.emp_no GROUP, e.ename

    ORDER BY e.emp_no

    ;

    Output of your sample data:

    EMP_NO ENAME SAL_TILL_2010 SAL_TILL_2014

    ---------- ------ ------------- -------------

    1 Mickey 450 0

    2 Donald 750 0

  • [8i] need help on query with a subquery/inline using UNION ALL view

    OK, first of all let's start with some background info:
    (1) I work in 8i
    (2) some samples for my problem (please excuse the additional columns of data not used to this problem; I already had the sample laying around tables):
    CREATE TABLE     vbom
    (
         part_nbr     char(25)     
    ,     bom_doc_nbr     char(25)     
    ,     bill_level     number          
    ,     comp_part_nbr     char(25)     
    ,     qty_per          number
    );
    --technically has primary and foreign keys, but whatever...
    --part_nbr is your top level (0) parent
    --comp_part_nbr is the specific child
    --bom_doc_nbr is the parent of the child (comp_part_nbr) and may be either part_nbr or a child of part_nbr that is also a parent
    
    INSERT INTO vbom
    VALUES ('SAMPLE-1','SAMPLE-1', 1,'SAMPLE-2',1);
    
    CREATE TABLE     rqmt
    (
         comp_part_nbr     char(25)     
    ,     prnt_part_nbr     char(25)
    ,     ord_nbr          char(10)     
    ,     sub_ord_nbr     char(3)
    ,     qty_reqd     number
    ,     qty_issued     number
    ,     date_reqd     date
    ,     rqmt_stat     char(2)
    ,     rqmt_type     char(2)
    );
    
    INSERT INTO rqmt
    VALUES ('SAMPLE-1','','S0000TEST1',001,30,0,TO_DATE('06/01/2010','mm/dd/yyyy'),'AL','ID');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000054963',001,15,10,TO_DATE('04/01/2010','mm/dd/yyyy'),'CL','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000032562',001,5,5,TO_DATE('04/15/2010','mm/dd/yyyy'),'IS','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000022341',001,5,4,TO_DATE('04/20/2010','mm/dd/yyyy'),'SH','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000043469',001,10,0,TO_DATE('04/30/2010','mm/dd/yyyy'),'AL','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000071235',001,10,0,TO_DATE('05/01/2010','mm/dd/yyyy'),'OP','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000061224',001,5,0,TO_DATE('05/15/2010','mm/dd/yyyy'),'FP','DD');
    INSERT INTO rqmt
    VALUES ('SAMPLE-2','SAMPLE-1','0000033294',001,5,0,TO_DATE('05/25/2010','mm/dd/yyyy'),'PL','DD');
    So, my first question was that I needed to find in the table RQMT who corresponded with everything on my VBOM have vbom.part_nbr table ' SAMPLE-1'. '. (Please note, in reality the VBOM table has thousands of rows of data, rather than just the 1 I have planned, some of them sharing the same vbom.part_nbr, others not). In addition to finding all RQMT data corresponding to the vbom.comp_part_nbr to vbom.part_nbr (where vbom.comp_part_nbr = rqmt.comp_part_nbr), I also need to find RQMT data for vbom.part_nbr itself (then, records where vbom.part_nbr = rqmt.comp_part_nbr).

    To resolve this problem, I started with the following query:
    SELECT     v.bill_level          AS bill_lvl
    ,     v.comp_part_nbr          AS component
    ,     v.bom_doc_nbr          AS parent
    FROM     VBOM v
    WHERE     v.part_nbr     ='SAMPLE-1'
    UNION ALL
    SELECT     0               AS bill_lvl
    ,     'SAMPLE-1'          AS component
    ,     NULL               AS parent
    FROM DUAL
    It allows me to add a line to the top of the page parent (vbom.part_nbr), which does not exist in vbom.
    My first question is, is UNION ALL of the best way to do it, or could I do a kind of instruction based on the line number box (or something) that would be better? (in this application, or in the final query)

    Then, I used the above query as a point of view online:
    SELECT     a.bill_lvl
    ,     a.component
    ,     a.parent
    ,     r.ord_nbr
    ,     r.sub_ord_nbr
    ,     r.qty_reqd
    FROM     (
         SELECT     v.bill_level          AS bill_lvl
         ,     v.comp_part_nbr          AS component
         ,     v.bom_doc_nbr          AS parent
         FROM     VBOM v
         WHERE     v.part_nbr     ='SAMPLE-1'
         UNION ALL
         SELECT     0               AS bill_lvl
         ,     'SAMPLE-1'          AS component
         ,     NULL               AS parent
         FROM DUAL
         ) a
    ,     RQMT r
    WHERE     a.component     = r.comp_part_nbr
    The problem here is that I have the same results (7 files) with the above query as I do with:
    SELECT     v.bill_level
    ,     v.comp_part_nbr
    ,     v.bom_doc_nbr
    ,     r.ord_nbr
    ,     r.sub_ord_nbr
    ,     r.qty_reqd
    FROM     VBOM v
    ,     RQMT r
    WHERE     v.comp_part_nbr     = r.comp_part_nbr
    AND     v.part_nbr     = 'SAMPLE-1'
    .. .it does not include RQMT recording for rqmt.comp_part_nbr = "SAMPLE-1'.

    To understand what was going on, I ran:
    SELECT     a.bill_lvl
    ,     a.component
    ,     a.parent
    FROM     (
         SELECT     v.bill_level          AS bill_lvl
         ,     v.comp_part_nbr          AS component
         ,     v.bom_doc_nbr          AS parent
         FROM     VBOM v
         WHERE     v.part_nbr     ='SAMPLE-1'
         UNION ALL
         SELECT     0               AS bill_lvl
         ,     'SAMPLE-1'          AS component
         ,     NULL               AS parent
         FROM DUAL
         ) a
    .. .and I get exactly this that I wait, 1 card for the SAMPLE-2 component and 1 plug for SAMPLE-1 (i.e. the recording I generated with the UNION ALL).

    Then I ran:
    SELECT     r.comp_part_nbr
    ,     r.ord_nbr
    ,     r.sub_ord_nbr
    ,     r.qty_reqd
    FROM     RQMT r
    WHERE     r.comp_part_nbr like 'SAMPLE-%'
    .. .and I get all 8 records, whose SAMPLE-1.

    So, it seems that the problem occurred when I join my opinion RQMT one online, although both separately seem to work very well.

    A point to note: all parts of parent and child may not be appointed in the same way as in my example. SAMPLE-1 could have children of the SAMPLE-2, abc123, 20735, for example.

    Any help on this would be appreciated, thanks!

    Hello

    Concerns of problem by comparing 8 literal characters ' SAMPLE-1' for the 25-character CHAR column that contains "SAMPLE 1".»
    17 spaces including the literal or use LPAD or CAST to 25 characters.
    For example:

    SELECT     a.bill_lvl
    ,     a.component
    ,     a.parent
    ,     r.ord_nbr
    ,     r.sub_ord_nbr
    ,     r.qty_reqd
    FROM     (
         SELECT     v.bill_level          AS bill_lvl
         ,     v.comp_part_nbr          AS component
         ,     v.bom_doc_nbr          AS parent
         FROM     VBOM v
         WHERE     v.part_nbr     ='SAMPLE-1'
         UNION ALL
         SELECT     0               AS bill_lvl
         ,     CAST ('SAMPLE-1' AS CHAR (25))               -- CAST added here
                                      AS component
         ,     NULL               AS parent
         FROM DUAL
         ) a
    ,     RQMT r
    WHERE     a.component     = r.comp_part_nbr
    ;
    

    Is there a reason why you use CHAR instead of VARCHAR2? (One reason other than "I can't change it now.", otherwise said.)

  • Order SQL ORA-00933 not correctly completed, UNION ALL

    I apologize if it's really simple, but it made me turn round...

    I inherited the spaghetti code to fix and revise (550 line sql request sent from VBA). I'm putting it in pieces in the form of views. In the following code, I'm calling two subqueries, which I stored in the form of views (Credit_Report_Exposure_P_Sub_A and Credit_Report_Exposure_P_Sub_B). I have create a UNION of them, and it works fine - until I add a WHERE clause. So:

    ---------------------------------------------------------------------------------
    SELECT
    PROFILENAME,
    months,
    sum

    Of
    Credit_Report_Exposure_P_Sub_A

    UNION ALL

    (SELECT
    ccp_profile_name PROFILENAME,
    VWMONTH month,
    SUM SUM (PSJ_TOTAL_AMOUNT)
    Of
    Credit_Report_Exposure_P_Sub_B
    GROUP by
    VWMONTH,
    ccp_profile_name)
    ------------------------------------------------------------------------------------------

    It works very well, to select the same 3 columns in each subquery (ignoring for a moment the wrong choice of alias 'months' and 'sum').

    But when I add the following WHERE clause:
    ------------------------------------------------------------------------------------------
    WHERE
    month = ' 2008-09'
    ------------------------------------------------------------------------------------------
    I get the error in the subject line - which is not properly completed.

    But: if I comment on each half of the UNION, so the WHERE clause works very well.

    Am I missing something obvious? I tried wrapping the whole of the UNION more in parentheses, and the first clause of the UNION as well, but that only creates more errors on supposed lack of right parens.

    Thanks for any help.

    Hello

    Whenever you have a problem with a query, view query. It is unclear where exactly you added the WHERE clause.

    You don't need parentheses around the second part of the UNION all.

    Half of the effect of the WHERE clause of the will UNION ALL? If both, you must add two times, or do the UNION ALL in a subquery and add the WHERE clause to the query of super.

  • Why UNION ALL allows no column names only when a literal is used and there is at least 3 parts of the query?

    An artificial union all:

    SQL> SELECT Dummy FROM Dual ORDER BY Dummy;
    
    D
    -
    X
    
    SQL> SELECT Dummy FROM Dual UNION ALL SELECT Dummy FROM Dual ORDER BY Dummy;
    
    D
    -
    X
    X
    
    SQL> SELECT Dummy FROM Dual UNION ALL SELECT Dummy FROM Dual UNION ALL SELECT Dummy FROM Dual ORDER BY Dummy;
    
    D
    -
    X
    X
    X
    
    
    

    It literally changes it:

    SQL> SELECT NULL A FROM Dual ORDER BY A;
    
    A
    -
    
    
    SQL> SELECT NULL A FROM Dual UNION ALL SELECT NULL FROM Dual ORDER BY A;
    
    A
    -
    
    
    
    SQL> SELECT NULL A FROM Dual UNION ALL SELECT NULL FROM Dual UNION ALL SELECT NULL FROM Dual ORDER BY A;
    SELECT NULL A FROM Dual UNION ALL SELECT NULL FROM Dual UNION ALL SELECT NULL FROM Dual ORDER BY A
                                                                                                     *
    ERROR at line 1:
    ORA-00904: "A": invalid identifier
    
    
    SQL> SELECT NULL A FROM Dual UNION ALL SELECT NULL FROM Dual UNION ALL SELECT NULL FROM Dual ORDER BY 1;
    
    A
    -
    
    
    
    
    

    Why 3 is the magic number?

    My friend, this is what I would call a "bug".

    However, my opinion doesn't matter, because I do not work for Oracle

    You opened a SR with Oracle?

  • With the help of SCORE on top of view with UNION

    Hi guys,.

    I explain what I'm trying to do it quickly:

    2 tables: table1 and table2 with the same structure and have both a multi_column_datastore ctxsys.context.

    1 view: View1

    Select * from table1

    Union

    Select * from table2

    If I run:

    Select * from View1 WHERE contains (view1. Column1, '% textext %', 1) > 0;

    It works fine, I get the correct result.

    If I try to use the PARTITION function, I got an error:

    Select * from View1 WHERE contains (view1. Column1, '% textext %', 1) > 0 ORDER by SCORE (1);

    ORA-29921: auxiliary operator not supported with query to configure block

    I understand the problem is in the UNION inside the view, is it possible to keep work, filtering the VIEW?

    Thanks in advance

    There is no score in the view, so you cannot reference the score when you query the view.  In order to put the note in the view, you need a contains the clause, which requires a value.  A method to do this is to use sys_context.  Please see the reproduction of the problem and solution below.

    Scott@orcl12c >-reproduction of the problem:

    Scott@orcl12c > create the table1 table:

    2 (column1 varchar2 (30))

    3.

    Table created.

    Scott@orcl12c > insert into table1 values ('textext")

    2.

    1 line of creation.

    Scott@orcl12c > create table table2

    2 (column1 varchar2 (30))

    3.

    Table created.

    Scott@orcl12c > insert into table2 values ('textext")

    2.

    1 line of creation.

    Scott@orcl12c > start

    2 ctx_ddl.create_preference ('test_ds', 'multi_column_datastore');

    3 ctx_ddl.set_attribute ('test_ds', 'columns', "column1");

    4 end;

    5.

    PL/SQL procedure successfully completed.

    Scott@orcl12c > create index table1_idx on table1 (column1)

    2 indextype is ctxsys.context

    3 parameters ("test_ds of the data store")

    4.

    The index is created.

    Scott@orcl12c > create index table2_idx on the table2 (column1)

    2 indextype is ctxsys.context

    3 parameters ("test_ds of the data store")

    4.

    The index is created.

    Scott@orcl12c > create or replace view View1

    2 as

    3 select * from table1

    4 union

    5 select * from table2

    6.

    Created view.

    Scott@orcl12c > select * from View1 where contains (view1.column1, '% textext %', 1) > 0

    2.

    COLUMN1

    ------------------------------

    textext

    1 selected line.

    Scott@orcl12c > select * from View1 where contains > 0 (view1.column1,'%textext%',1) order by score (1)

    2.

    Select * from View1 where contains > 0 (view1.column1,'%textext%',1) order by score (1)

    *

    ERROR on line 1:

    ORA-29921: auxiliary operator not supported with query to configure block

    Scott@orcl12c >-solution:

    Scott@orcl12c > create or replace view View1

    2 as

    3. Select the partition (1) score, table1.* from table1

    4 where contains (table1. Column1, sys_context ('text_query', 'query_value'), 1) > 0

    5 union

    6 select score partition (1), table2.* from table2

    7 where contains (table2.column1, sys_context ('text_query', 'query_value'), 1) > 0

    8.

    Created view.

    Scott@orcl12c > create or replace context text_query using text_proc

    2.

    Context that is created.

    Scott@orcl12c > create or replace procedure text_proc

    2 (p_val in varchar2)

    3 as

    4 start

    5 dbms_session.set_context ('text_query', "query_value", p_val);

    6 end text_proc;

    7.

    Created procedure.

    Scott@orcl12c > text_proc exec ('% textext %')

    PL/SQL procedure successfully completed.

    Scott@orcl12c > set autotrace on explain

    Scott@orcl12c > select * from View1 by score

    2.

    MARK THE COLUMN1

    ---------- ------------------------------

    3 textext

    1 selected line.

    Execution plan

    ----------------------------------------------------------

    Hash value of plan: 4090246122

    ----------------------------------------------------------------------------------------------

    | ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time |

    ----------------------------------------------------------------------------------------------

    |   0 | SELECT STATEMENT |            |     2.    60.     8 (0) | 00:00:01 |

    |   1.  SORT ORDER BY |            |     2.    60.     8 (0) | 00:00:01 |

    |   2.   VIEW                          | VIEW1.     2.    60.     8 (0) | 00:00:01 |

    |   3.    UNIQUE FATE |            |     2.    58.     8 (50) | 00:00:01 |

    |   4.     UNION-ALL |            |       |       |            |          |

    |   5.      TABLE ACCESS BY INDEX ROWID | TABLE1.     1.    29.     4 (0) | 00:00:01 |

    |*  6 |       DOMAIN INDEX | TABLE1_IDX |       |       |     4 (0) | 00:00:01 |

    |   7.      TABLE ACCESS BY INDEX ROWID | TABLE2.     1.    29.     4 (0) | 00:00:01 |

    |*  8 |       DOMAIN INDEX | TABLE2_IDX |       |       |     4 (0) | 00:00:01 |

    ----------------------------------------------------------------------------------------------

    Information of predicates (identified by the operation identity card):

    ---------------------------------------------------

    6 - access("CTXSYS".") CONTAINS "(" TABLE1".»)" Column1', SYS_CONTEXT ('text_query ',' query_v)

    Alou '), 1) > 0)

    8 - access("CTXSYS".") CONTAINS "(" TABLE2".»)" Column1', SYS_CONTEXT ('text_query ',' query_v)

    Alou '), 1) > 0)

    Note

    -----

    -the dynamic statistics used: dynamic sampling (level = 2)

    Scott@orcl12c >

  • Problem with Union

    Hello

    I have a request below which I need the merger of two of them to get the result in a single query result, here's the query
    but the error me do is ORA-12704: incompatibility of character set, please let me know wat is the error in the query below

    Select rpad ('Roster_num', 20, "") 'Roster_num '.
    of the double

    UNION ALL

    SELECT
    SEPARATE
    RPAD (AP. EMPLOYEE_CODE, 20, "") 'Roster_num '.

    AR_EMPLOYEE_PERSONAL AP,
    AR_ADDRESS AA,
    AR_V_ROSTER_EMPLOYEE_DEF AD,
    AR_STATE_PROV_REGION ASP,
    AR_DEFINITION_TERRITORY ADT

    WHERE
    BDM PROJECT = 81 AND
    BPM EMPLOYEE_ID = AA. EMPLOYE_ID AND
    BPM EMPLOYEE_ID = AD. EMPLOYEE_ID (+) AND
    BAM STATE_PROV_REG_ID = ASP STATE_PROV_REG_ID AND
    BDM DEFINITION_TERRITORY_ID = ADT. DEFINITION_TERRITORY_ID (+)



    Thank you
    Sudhir

    Explanation with some examples:

    SQL>desc t_test;
    Name                           Null     Type
    ------------------------------ -------- -----------------
    COL1                                    NUMBER
    COL2                                    NUMBER
    COL3                                    NUMBER
    COL4                                    NCHAR(30)   
    

    We see that COL4 in the table is of type NCHAR.

    SQL>select * from t_test;
    COL1                   COL2                   COL3                   COL4
    ---------------------- ---------------------- ---------------------- ------------------------------
    1                      123                    3                      Roster
    2                      3                      4                      RKött
    3                      4                      2
    9                      10                     12
    11                     23                     43                                                    
    
    SQL>select rpad('Roster_num',20,' ') "Roster_num"
    from dual
    union all
    select RPAD(col4,20,' ')  "Roster_num" from t_test;
    
    Error starting at line 7 in command:
    select rpad('Roster_num',20,' ') "Roster_num"
    from dual
    union all
    select RPAD(col4,20,' ')  "Roster_num" from t_test
    Error at Command Line:7 Column:7
    Error report:
    SQL Error: ORA-12704: character set mismatch
    12704. 00000 -  "character set mismatch"
    *Cause:    One of the following
               - The string operands(other than an nlsparams argument) to an
               operator or built-in function do not have the same character
               set.
               - An nlsparams operand is not in the database character set.
               - String data with character set other than the database character
               set is passed to a built-in function not expecting it.
               - The second argument to CHR() or CSCONVERT() is not CHAR_CS or
               NCHAR_CS.
               - A string expression in the VALUES clause of an INSERT statement,
               or the SET clause of an UPDATE statement, does not have the
               same character set as the column into which the value would
               be inserted.
               - A value provided in a DEFAULT clause when creating a table does
               not have the same character set as declared for the column.
               - An argument to a PL/SQL function does not conform to the
               character set requirements of the corresponding parameter.
    *Action:
    

    Error returned for incompatibility of character as data types are not compatible.

    SQL>select rpad('Roster_num',20,' ') "Roster_num"
    from dual
    union all
    select TO_CHAR(RPAD(col4,20,' '))  "Roster_num" from t_test;
    
    Roster_num
    ------------------------------------------
    Roster_num
    Roster
    RKött                                                                                                                                                                                                    
    
    6 rows selected
    SQL>
    

    After the converted to the TO_CHAR problem solved

  • Invalid identifier error using UNION ALL?

    Can someone tell me why it gives me the error: ORA-00904: "Wait_time": invalid identifier.

    It must be something with the UNION ALL because it works fine if I have only one query at a time.

    Thank you
    Deanna



    Select wait_time | ' Minutes.' | «, » || COUNTY (wait_time) under the name of "0 to 10 Min, COUNT"
    Of MOTOR_ASSIST2
    WHERE the wait_time BETWEEN 0 AND 10 and ((: p53_fiscal_yr IS NULL))
    GOLD: p53_fiscal_yr = TO_CHAR (ADD_MONTHS (datetime, 6), "YYYY"))
    OR (: p53_month IS NULL or: P53_month = TO_CHAR (DATETIME, 'MM'))
    (AND (: p53_year IS NULL or: p53_year = TO_CHAR (DATETIME, 'YYYY')))

    UNION ALL

    Select wait_time | ' Minutes.' | «, » || COUNTY (wait_time) under the name of "11-30 Min, COUNT.
    Of MOTOR_ASSIST2
    where wait_time BETWEEN 11 AND 30 and ((: p53_fiscal_yr IS NULL))
    GOLD: p53_fiscal_yr = TO_CHAR (ADD_MONTHS (datetime, 6), "YYYY"))
    OR (: p53_month IS NULL or: P53_month = TO_CHAR (DATETIME, 'MM'))
    (AND (: p53_year IS NULL or: p53_year = TO_CHAR (DATETIME, 'YYYY')))

    UNION ALL

    Select wait_time | ' Minutes.' | «, » || COUNTY (wait_time) under the name of "31-60 Min, COUNT.
    of MOTOR_ASSIST2
    where wait_time BETWEEN 31 AND 60 and ((: p53_fiscal_yr IS NULL))
    GOLD: p53_fiscal_yr = TO_CHAR (ADD_MONTHS (datetime, 6), "YYYY"))
    OR (: p53_month IS NULL or: P53_month = TO_CHAR (DATETIME, 'MM'))
    (AND (: p53_year IS NULL or: p53_year = TO_CHAR (DATETIME, 'YYYY')))

    UNION ALL

    Select wait_time | ' Minutes.' | «, » || (WAIT_TIME) count as '61 Min + COUNT.
    of MOTOR_ASSIST2
    where wait_time BETWEEN 61 AND 1000 and ((: p53_fiscal_yr IS NULL))
    GOLD: p53_fiscal_yr = TO_CHAR (ADD_MONTHS (datetime, 6), "YYYY"))
    OR (: p53_month IS NULL or: P53_month = TO_CHAR (DATETIME, 'MM'))
    (AND (: p53_year IS NULL or: p53_year = TO_CHAR (DATETIME, 'YYYY')))


    WAIT_TIME GROUP
    ORDER BY WAIT_TIME;

    And if you want to be really want...

    SQL> ed
    Wrote file afiedt.buf
    
      1  WITH c1 as (select row_number() over (order by sal) as rn
      2                    ,SAL||':'||COUNT(*) "SAL0-999"
      3              from emp
      4              where sal between 0 and 999
      5              group by sal)
      6      ,c2 as (select row_number() over (order by sal) as rn
      7                    ,SAL||':'||COUNT(*) "SAL1000-1999"
      8              from emp
      9              where sal between 1000 and 1999
     10              group by sal)
     11      ,c3 as (select row_number() over (order by sal) as rn
     12                    ,SAL||':'||COUNT(*) "SAL2000-2999"
     13              from emp
     14              where sal between 2000 and 2999
     15              group by sal)
     16      ,c4 as (select row_number() over (order by sal) as rn
     17                    ,SAL||':'||COUNT(*) "SAL3000-3999"
     18              from emp
     19              where sal between 3000 and 3999
     20              group by sal)
     21      ,c5 as (select row_number() over (order by sal) as rn
     22                    ,SAL||':'||COUNT(*) "SAL4000+"
     23              from emp
     24              where sal >= 4000
     25              group by sal)
     26  select "SAL0-999", "SAL1000-1999", "SAL2000-2999", "SAL3000-3999", "SAL4000+"
     27  from c1 FULL OUTER JOIN c2 ON (c2.rn = c1.rn)
     28          FULL OUTER JOIN c3 ON (c3.rn = COALESCE(c1.rn, c2.rn))
     29          FULL OUTER JOIN c4 ON (c4.rn = COALESCE(c1.rn, c2.rn, c3.rn))
     30          FULL OUTER JOIN c5 ON (c5.rn = COALESCE(c1.rn, c2.rn, c3.rn, c4.rn))
     31* order by 1,2,3,4,5
    SQL> /
    
    SAL0-999       SAL1000-1999   SAL2000-2999   SAL3000-3999   SAL4000+
    -------------- -------------- -------------- -------------- --------------
    800:1          1100:1         2450:1         3000:2         5000:1
    950:1          1250:2         2850:1
                   1300:1         2975:1
                   1500:1
                   1600:1
    
    SQL>
    

    Published by: BluShadow on November 7, 2008 15:47
    Elimination of unnecessary case statements

Maybe you are looking for