Select different columns using union all

I'm trying to select all the records of two table

Select A, B, C from table1
Union of all the
Select A, B, D, E, F from table2

I get results A, B, C columns but not D, E, F.

What should I do to get all the columns in the result. as A, B, C, D, E, F

Thank you
SK

Hello

That's what you asked for:

select         Total_Customer_Qty,  NULL AS Total_Supplier_Qty,  Item_number,  Org,item_id  from table1;
union all
select NULL AS Total_Customer_Qty,          Total_Supplier_Qty,  Item_number,  Org,item_id  from table2;

I hope that's what you want, too.
If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all tables and also post the results desired from these data.
Explain, using specific examples, how you get these results from these data.
Always tell what version of Oracle you are using.

Tags: Database

Similar Questions

  • 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

  • [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.)

  • 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
    
  • 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

  • Select the column using

    Hello

    I have a report with 4 columns, 'Year', 'Demand amount', 'Amount of the purchase order' and 'invoice amount.

    The user want to select "Amount of demand", "The purchase order amount" or "Invoice amount" in the drop down menu from the drop in the Oracle replies

    In other words, he wants to see only the year and "amount of demand."
    year or "amount in the purchase order.
    year or "amount of the invoice.

    How can I do? I already search column, select but did not understand. Please tell me in detail.

    Thank you

    Hello user.

    I advise to use the column picker in the responses.

    (1) build your table (or any other point of view) a year and the amount of the claim.
    (2) then create a view by using the choice of mode 'column selector.
    (3) check the checkbox 'Include the selector' for your column 'amount of demand. "
    (4) add the other two columns in the selection pane. Your view should look like the image below:

    ! http://i46.Tinypic.com/2gwyfso.jpg!

    5) go to your compound view and add your views 'column selector.

    You should now be able to Exchange "Amount of demand", "The purchase order amount" and "Amount of the invoice" using the column Chooser, and your other points of view should replace columns that you select.

    I hope this helps and please assign points if you found it useful!

    Kind regards
    Jason

  • 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?

  • 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
    ;
    
  • 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

  • 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... ».

  • Union all too slow for my query any alternative?

    Hello

    The following query prints the desired results. What I'm trying to do is to add to my query below is to also retrieve information from different dates (for example 01.01.2012 - 30.4.2012), more information below, but with 0 amounts. The only solution I found is to duplicate the query to a union and entry dates you want below. The problem is that, then the query is too slow.
    Any other recommendations are welcome
    SELECT s.trans_datetime,s.alloc_ref,s.accnt_code,s.treference,s.DESCRIPTN,sum(s.amount*(-1)) as amount,s.conv_code,sum(s.other_amt*(-1)) as other_amt,s.anal_t1,ss.descr,
    o.NTN_DESCR as flag,z.name as vessel_name
    FROM  accounts s, customers ss,vessel a,nation o, analysis  z,
    customers_anl_cat  z1
    where s.trans_datetime BETWEEN to_date('01.01.2013','DD.MM.YYYY') AND to_date('30.04.2013','DD.MM.YYYY')
    AND s.accnt_code=z1.acnt_code
    AND z1.anl_cat_id=17
    AND ss.ACNT_TYPE=2
    AND s.accnt_code=ss.acnt_code
    AND o.NTN_CODE=a.flg_code
    AND z.anl_code=s.anal_t1
    GROUP BY s.trans_datetime,s.alloc_ref,s.accnt_code,s.treference,s.DESCRIPTN,s.conv_code,s.anal_t1,ss.descr
    ,o.NTN_DESCR,z.name,z1.anl_code
    Thanks in advance

    794018 wrote:
    Thank you very much for your answers.

    Unfortunately, the lines of the new date in the amount and need to convert to 0 or null values

    OK... What is not eligible for a situation to replicate the query to use UNION all clause You can simply do more in select:

    Select  column_list1... column_listN,
    case when s.trans_datetime BETWEEN to_date('01.01.2013','DD.MM.YYYY') AND to_date('30.04.2013','DD.MM.YYYY') then s.amount
           when s.trans_datetime BETWEEN to_date('01.01.2012','DD.MM.YYYY') AND to_date('30.04.2012','DD.MM.YYYY') then 0
    end amount
    from table_names
    where (s.trans_datetime BETWEEN to_date('01.01.2013','DD.MM.YYYY') AND to_date('30.04.2013','DD.MM.YYYY') or s.trans_datetime BETWEEN to_date('01.01.2012','DD.MM.YYYY') AND to_date('30.04.2012','DD.MM.YYYY'))
    Other conditions Follow
    
  • How to upgrade a table column using the values in the Oracle collection

    create or replace procedure test_coll
    
    IS
    
    CURSOR upd 
    IS
    SELECT CONTACT_NAME FROM Supplier_16;
    
    TYPE dept IS TABLE OF upd%rowtype;
    cur_var dept;
    
    Type List Is table Of  varchar2(20);
    Name List:=  List('Shilpi','Sunil','Shreyas','Saral');
    
    BEGIN
    
    OPEN upd;
    LOOP
         FETCH upd BULK COLLECT INTO cur_var;
    --    EXIT WHEN upd%NOTFOUND;
    
         FORALL i IN cur_var.FIRST..cur_var.LAST
      
         UPDATE supplier_16
    **  SET Contact_name= name(i);  ***
         
         COMMIT;
    
    END LOOP;
    CLOSE upd;
    
    END;
    On the "BOLD" line, I don't know how I should move the values of the collection of name I said without which are set all the values in the table supplier_16 = 'Saral.

    Help, please.

    Aashish S. wrote:
    Thank you very much...

    Yes, I slide collections and was trying to reach somwthing on similar lines to which you provided the code example...

    My essay is equipped to take a collection: initialized with values of say 3-4...

    Take other tables... A column... and update the column in the table (not PK, FK anything) using the values of the initialized collection...

    However, I am stuck between the two on how the UPDATE clause should be...

    OK, if it's just because you want to practice with collections, you might do something like this...

    SQL> set serverout on
    SQL> create table supplier_16 as
      2  select 'Frederick' as contact_name from dual union all
      3  select 'Robert' from dual union all
      4  select 'Jeremy' from dual union all
      5  select 'Simon' from dual
      6  /
    
    Table created.
    
    SQL> create or replace procedure test_coll is
      2    CURSOR upd IS
      3      SELECT CONTACT_NAME
      4      FROM Supplier_16
      5      FOR UPDATE;
      6    Type List Is table Of  varchar2(20);
      7    Name List := List('Shilpi','Sunil','Shreyas','Saral');
      8    v_contact_name varchar2(30);
      9    v_idx          number := 1;
     10  BEGIN
     11    OPEN upd;
     12    LOOP
     13       FETCH upd INTO v_contact_name;
     14       EXIT WHEN upd%NOTFOUND;
     15       UPDATE supplier_16
     16       SET    contact_name = name(v_idx)
     17       WHERE CURRENT OF upd;
     18       DBMS_OUTPUT.PUT_LINE(v_contact_name||' update to '||name(v_idx));
     19       v_idx := v_idx + 1;
     20    END LOOP;
     21    CLOSE upd;
     22    COMMIT;
     23  END;
     24  /
    
    Procedure created.
    
    SQL> exec test_coll;
    Frederick update to Shilpi
    Robert update to Sunil
    Jeremy update to Shreyas
    Simon update to Saral
    
    PL/SQL procedure successfully completed.
    
    SQL> select * from supplier_16;
    
    CONTACT_N
    ---------
    Shilpi
    Sunil
    Shreyas
    Saral
    

    Of course, there is treatment rank by rank and is not best for performance, but it allows you to access your collection that you created names.

  • can we use to_date using Union or Union

    Hi, while I do any union with two tables I get

    ORA-01790: expression must have same type of data, matching expression
    01790 00000 - "expression must have the same type of data, matching expression.


    and if I change this data type of TO_CHAR is showing correct result

    SELECT (TO_CHAR (C61_CURRENT_MONTH, ' MON-DD-YYYY "") CURRENT_MONTH EDW.) C$ _0VW_TEMP_PLAYER_DTL_LOAD
    UNION ALL
    SELECT (TO_CHAR (C17_CURRENT_MONTH, ' MON-DD-YYYY "") CURRENT_MONTH EDW.) C$ _1VW_TEMP_PLAYER_DTL_LOAD

    and I'm going to insert this result in a table which column is defined as the DATE data type

    so I tried below one as

    Select to_date (TO_CHAR (C61_CURRENT_MONTH, ' MON-DD-YYYY ""), 'dd-mon-yyyy') CURRENT_MONTH OF EDW. C$ _0VW_TEMP_PLAYER_DTL_LOAD
    UNION ALL
    TO_DATE (TO_CHAR (C17_CURRENT_MONTH, ' MON-DD-YYYY ""), 'dd-mon-yyyy') CURRENT_MONTH OF EDW. C$ _1VW_TEMP_PLAYER_DTL_LOAD

    but I get the error message like

    ORA-01790: expression must have same type of data, matching expression

    so my question is can we use to_date using union all.

    Maybe do you the conversion to remove time from date values.

    This could be done better with:

    select trunc(C61_CURRENT_MONTH) CURRENT_MONTH FROM EDW.C$_0VW_TEMP_PLAYER_DTL_LOAD
    UNION ALL
    select trunc(C17_CURRENT_MONTH) CURRENT_MONTH FROM EDW.C$_1VW_TEMP_PLAYER_DTL_LOAD
    

    I can't reproduce your original error, because I do not have your tables (test data would be nice)
    and no doubt I'm on another database-version (we do not know your version of db).

    Published by: hm on 19.04.2012 04:30

  • 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

  • Select different schema data

    I have 3 users in the schema.
    User09
    User10
    User11
    PURCHASE table name exists in all these three schema with different data (depending on date)
    As
    User09-> purchase - data date Jan09 to dec09
    User10-> purchase - data date Jan10 Dec10
    User11-> purchase - data date Jan11 Dec11

    I want to choose * purchase, on any user select three with education.

    I choose data with union all
    select * from user09.pruchase Union all
    select * from user10.pruchase Union all
    select * from user11.pruchase
    It is a problem: -.
    After you create this query I have add another utilisateur12 user and that the user has the same purchase table so I'll have to add another line IN Union all question them.

    When I see this query. It gives me the name of all the owner of User09, User10, User11...
    SELECT * FROM ALL_ALL_TABLES
    WHERE OWNER LIKE 'USER%'
    and table_name ='PURCHASE'
    
    OWNER                          TABLE_NAME                     TABLESPACE_NAME                
    ------------------------------ ------------------------------ ------------------------------ 
    USER                            PURCHASE                       USERS                         
    USER09                          PURCHASE                       USERS                         
    USER10                          PURCHASE                       USERS                         
    3 rows selected
    Can I get purchase data Table of all users with the statement select.

    Line 17 must be

    End Loop;
    

    Notice the semicolon sign.

Maybe you are looking for