Union two Tables

Hello

I have two tables with the same data structures and I want the two to the union. The problem I faced with union is the results are joined and it is difficult to distinguish which set of result belongs to which table:

for example

Select * from staff
Union
Select * from employees

Jan - 06 Division
Activity 10
Company 4
6 marketing
Marketing 3
5 finance
Finance 2

Is there a way to display the above results like

Jan - 06 Division
"Staff".
Activity 10
6 marketing
5 finance
"Outgoing".
Company 4
Marketing 3
Finance 2

The difference is there is a line break, showing the separation of staff and outgoing. Can anyone help?

Thank you

Does that help?

select 'STAFF' TABLE_NAME, s.* from staff s
union
select 'EMPLOYEES' TABLE_NAME, e.* from employees e

Tags: Database

Similar Questions

  • getting data between two tables

    Hi everyone, I need help with a query that seems simple, but I can't understand. consider the following data.

    with data as (

    s élire 802 cd from dual union all

    select 803 cd from dual union all

    select 804 cd from dual

    data2 as

    ( select 804 cd from dual )


    I want to join the two tables so that the result will be as follows:

    in the above data, we can see that 804 is common in both tables.  in this case, the output should be 804.

    Similarly, if 2 data include the following:

    data2 as

    ( select 804 cd from dual union all

    Select 803 cd from dual

    )


    the output is then 803 804.



    Now consider the following data

    with data as (

    s élire 802 cd from dual union all

    select 803 cd from dual union all

    select 804 cd from dual

    )

    take data2 table is empty


    I want to join the two tables, and in this case, the output should be 802,803,804.  data2 table is empty, then the default behavior is to get all the data in the data table. Since there is no line that corresponds to rows in the data table,

    the default behavior is to display output all data in the data table.  in this case 802,803,804

    can someone write a query for this?

    Hello

    elmasduro wrote:

    Hi Frank, thanks for the request.  These are very good questions. Here are the answers

    What happens if data2 rows, but none of them match data CD?

    It won't happen because the integrity of the data.

    ...

    I see it; a data line 2 will be in the data.  You have a FOREIGN KEY constraint, or something similar, so if cd = 804 exists in database2, then it must also exist in the data.

    What happens if the data is empty and data2 is not?

    If data is empty and data2 is not, then the data2 data should be released.

    for example

    with the data as)

    data table is empty

    data2 as

    (select cd 804 Union double all the)

    Select cd 805 double

    )

    When you join data and data2 account with the above data, then the output should be 804 805.  data2 data should be the display if the data table is empty. ...

    Doesn't that contradict what you said earlier?  Previously, it seemed to say that each row of data2 will be in the data, but now you give an example where all data2 lines are not in the data.

    If the first is true, then the solution in response #2 should work.

    If the latter is true, you can change the query like this:

    WITH got_r_num AS

    (

    SELECT NVL (d.cd, d2.cd) AS cd

    DENSE_RANK () (ORDER IN CASE

    WHEN d.cd IS NOT NULL

    AND d2.cd IS NOT NULL

    THEN "A".

    WHEN d.cd IS NOT NULL

    THEN 'B '.

    ANOTHER 'C '.

    END

    ) AS r_num

    DATA d

    FULL OUTER JOIN data2 d2 ON d.cd = d2.cd

    )

    SELECT cd

    OF got_r_num

    WHERE r_num = 1

    ORDER BY cd

    ;

  • Delete rows in a table when the columns from two tables match

    Hello

    I have following two tables.

    ===========================================

    create the table empbooth as

    (

    Select 1 empid, 1 double cabin Union all the

    Select option 2, Union 1 double all the

    Select 3, Union 1 double all the

    Select option 4, Union 2 double all the

    Select option 5, 2 double

    );

    create the table attsht as

    (

    Select 1 empid, 240 reg, 0 unpaid all double union

    Select option 2, reg 200, 0 unpaid of all the double union

    Select 3, 240 reg, 0 unpaid all double them union

    Select 4 480 reg, 0 unpaid all double union

    Select 5 240 reg, unpaid double 0

    );

    =================================================

    I want to remove rows from attsht where corresponding booth (which is stored in the empbooth table) is 1.

    The condition is 'where attsht.empid = empbooth.empid and empbooth.booth = 1 '.

    I use oracle 10g.

    Help, please

    delete from attsht where a.empid in (select b.empid from empbooth b where b.booth = 1)

    or

    remove from attsht a

    where exists (select null

    of empbooth b

    where b.booth = 1

    and b.empid = a.empid)

  • Valid time intervals: two tables intersect documents

    Benefits:

    I have two tables:

    AVAILABILITY (ALLOCATION A_ID, START_TIME, END_TIME, ID_RESSOURCE)

    TASK (T_ID, START_TIME, END_TIME, ID_RESSOURCE)

    My final request should print:

    Id_ressource, DAY_OF_WEEK, MYDATE, DURATION (in hours)

    Semantics back: "give me all the time slots available; Time slot available to the general public is the time range that does not contain a task"

    1. in the tables above START_TIME, END_TIME are of type DATE and they contain a timestamp that is to say:

    SELECT to_char (START_TIME, 'HH24 MMDDYYYY') from the AVAILABILITY/TASK

    would print something like

    09232014 14:17

    2. in the tables above time START_TIME and END_TIME are of the same date and END_TIME > START_TIME IE

    START_TIME = 09232014 14:17

    END_TIME = 09232014 15:17

    no need to check that

    3. If for some date there are TASK BOTH records and AVAILABILITY, then it must be:

    TASK. End_time < = AVAILABILITY. End_time and TASK. Start_time = > AVAILABILITY. START_TIME

    In other words, JOB registration is still "inside" free and BUSY folder

    I need time slots which are essentially output

    -All available records that do not contain a ' ' tasks.

    AVAILABILITY. Id_ressource, to_char (AVAILABILITY. Start_time, 'DAY') as DAY_OF_WEEK, AVAILABILITY. Start_time as MYDATE, (AVAILABILITY. END_TIME-AVAILABILITY. Start_time) * 24 as DURATION

    AND

    -If the free and busy folder contains task (see 3) then there are 2 two time intervals resulting (Yes the duration can be 0)

    AVAILABILITY. Id_ressource, to_char (AVAILABILITY. Start_time, 'DAY') as DAY_OF_WEEK, AVAILABILITY. Start_time as MYDATE, (TASK. START_TIME-AVAILABILITY. Start_time) * 24 as DURATION

    and

    AVAILABILITY. Id_ressource, to_char (AVAILABILITY. Start_time, 'DAY') as DAY_OF_WEEK, AVAILABILITY. Start_time as MYDATE, (AVAILABILITY. END_TIME-TASK. End_time) * 24 as DURATION

    Hope the above makes sense. What is the best, more rapid SQL?

    Hello

    Here's a way to do it:

    WITH union_data AS

    (

    SELECT id_ressource, change_time, change_val

    AVAILABILITY

    UNPIVOT (change_time

    FOR change_val IN (start_time AS 1

    end_time AS - 1

    )

    )

    UNION ALL

    SELECT id_ressource, change_time, change_val

    TASK

    UNPIVOT (change_time

    FOR change_val IN (start_time AS 1

    end_time AS - 1

    )

    )

    )

    got_change_sum AS

    (

    SELECT id_ressource

    change_time

    SUM (change_val) OVER (PARTITION BY id_ressource

    ORDER BY change_time

    ) AS change_sum

    Advance (change_time) OVER (PARTITION BY id_ressource

    ORDER BY change_time

    ) - change_time AS long

    Of union_data

    )

    SELECT id_ressource

    , TO_CHAR (change_time, 'fmDay, FMMM-DD-YYYY') AS available_date

    Duration * 24 AS available_hours

    OF got_change_sum

    WHERE change_sum = 1

    ORDER BY id_ressource

    change_time

    ;

    To see how it works, view the results of each auxiliary request individually.

    You will see that the union_data product has all the points where either avaialablity or a task changes.  The column change_val is + 1 availability or a task at the beginning and - 1, when it ends.  When the cumulative sum of those is 0, this means that there is no availability.  When the accumulated amount is > 1, this means a task is underway.  We are interested in the places where the cumulative sum of change_val is exactly 1, sense that a period which started (but not yet completed) and that there is a net 0 current tasks.

    This solution is NOT the value of start_time and end_time in the same day of calendar and tasks for the same resource may overlap.

  • Take the difference of two tables

    Hello

    I have two tables where in I need to take the difference between the two columns of different tables and show as decimal below:

    Table A

    Name, salary

    x 10.5

    y - 10.32

    z 5

    Table B

    Name, salary

    x 10.5

    y - 10.32

    z 3

    a 12

    Output:

    Name_A, Name_B, salary, remarks

    x, x, 0, no diff

    y, y, 0, no diff

    z, z, 2 Diff is here

    , one, no data, no data from the previous

    Hello

    Try ythis:

    with table_a as
    (
    SELECT 'X' NAME, SALARY, 10.5 DUAL UNION ALL
    SELECT THE NAME OF 'Y',-10.32 SALARY OF DOUBLE UNION ALL
    SELECT THE NAME OF 'Z', 5 DOUBLE SALARY
    )
    table_B as
    (
    SELECT 'X' NAME, SALARY, 10.5 DUAL UNION ALL
    SELECT THE NAME OF 'Y',-10.32 SALARY OF DOUBLE UNION ALL
    SELECT THE NAME OF 'Z', 3 DOUBLE UNION ALL SALARIES
    SELECT 'A' NAME, SALARY 12 DUAL FROM
    )
    SELECT
    COALESCE(A.NAME,B.NAME) NAME
    , NVL (TO_CHAR (A.SALARY - B.SALARY), 'NO DATA') SALARY
    , CASE WHEN A.SALARY - B.SALARY = 0 THEN 'NO DIFFERENCE '.
    WHEN B.SID IS NULL, "NO DATA OF THE PREVIOUS.
    WHERE B.NAME IS NULL, "NO DATA FROM NEXT.
    ANOTHER 'DIFFERENCE IS THERE.
    END NOTE

    Of
    TABLE_A HAS
    TABLE_B FULL OUTER JOIN B ON (B.SID = B.NAME)
    ;

    NAME SALARY NOTE
    ---- ---------------------------------------- ---------------------
    X 0 NO DIFFERENCE
    Y 0 NO DIFFERENCE
    Z 2 THE DIFFERENCE IS THERE
    A NUMBER OF DATA NO DATA FROM THE PREVIOUS

    Kind regards

    Peter

  • JOIN the question... Join two tables without omitting lines

    I came across a problem that should be an easy fix (I hope), but I'm having a hard time to come up with a solution.

    Basically I have two tables, one with the actual amounts with the budget. I have to write a sql statement select that joins these tables together and includes all of their lines. I was able to join the tables by using JOIN, LEFT JOIN and RIGHT JOIN, but it always fails the lines I need.

    Below, I have examples of my tables (AMOUNT_TABLE and BUDGET_TABLE). For simplicity, I built the examples to show the same values in the first four columns, with the 5th and 6th columns (SUB_ACCOUNT, AMOUNT, BUDGET) as the only values that are different. My actual tables are not quite that simple, but I don't think it was relavent to this issue.

    AMOUNT_TABLE

    FISCAL_YEAR PERIOD ACCT_UNIT ACCOUNT SUB_ACCOUNT AMOUNT

    2013

    111111555555000010020131111115555551000100201311111155555520001002013111111555555300010020131111115555554000100

    BUDGET_TABLE

    FISCAL_YEAR PERIOD ACCT_UNIT ACCOUNT SUB_ACCOUNT BUDGET

    2013

    1111115555553000200201311111155555540002002013111111555555500020020131111115555556000200

    Here's what I hope. Note that SUB_ACCOUNTs 0000, 1000, and 2000 show without budget amounts since there is not a corresponding line in the BUDGET_TABLE. And same for SUB_ACCOUNTs 5000 and 6000, they show budgets with no amount because there is not a corresponding line in the AMOUNT_TABLE.

    (exit)

    FISCAL_YEAR PERIOD ACCT_UNIT ACCOUNT SUB_ACCOUNT AMOUNT BUDGET
    201311111155555500001000
    201311111155555510001000
    201311111155555520001000
    20131111115555553000100200
    20131111115555554000100200
    201311111155555550000200
    201311111155555560000200

    If all goes well, my question is clear. Any help on this would be greatly appreciated. Thanks in advance.

    Use the join ANSI - FULL OUTER JOIN syntax:

    with amount_table like)

    Select fiscal_year 2013, 1 period, 11111 acct_unit, 555555, 0000 sub_account, amount 100 of all the double union

    Select 2013,1,11111,555555,1000,100 from all the double union

    Select 2013,1,11111,555555,2000,100 from all the double union

    Select 2013,1,11111,555555,3000,100 from all the double union

    Select double 2013,1,11111,555555,4000,100

    ),

    budget_table like)

    Select 1 time, 11111 acct_unit, account 555555, sub_account 3000, 2013 fiscal_year, budget of 200 Union double all the

    Select 2013,1,11111,555555,4000,200 from all the double union

    Select 2013,1,11111,555555,5000,200 from all the double union

    Select double 2013,1,11111,555555,6000,200

    )

    Select nvl (a.fiscal_year, b.fiscal_year) fiscal_year,

    period of NVL (a.period, b.period),

    NVL (a.acct_unit, b.acct_unit) acct_unit.

    account of NVL (a.Account, b.Account),

    NVL (a.sub_account, b.sub_account) sub_account.

    Amount NVL(a.amount,0),

    NVL(b.budget,0) budget

    of amount_table one

    full join

    budget_table b

    on)

    a.Fiscal_Year = b.fiscal_year

    and

    a.period = b.period

    and

    a.acct_unit = b.acct_unit

    and

    a.Account = b.account

    and

    a.sub_account = b.sub_account

    )

    /

    FISCAL_YEAR PERIOD ACCT_UNIT ACCOUNT SUB_ACCOUNT AMOUNT BUDGET
    ----------- ---------- ---------- ---------- ----------- ---------- ----------
    2013 1 11111 555555 0 100 0
    2013 1 11111 555555 1000 100 0
    2013 1 11111 555555 2000 100 0
    2013 1 11111 555555 3000 100 200
    2013 1 11111 555555 4000 100 200
    2013 1 11111 555555 6000 0 200
    2013 1 11111 555555 5000 0 200

    7 selected lines.

    SQL >

    SY.

  • Merge two tables to a view

    I have two tables I have merged into a single view.
    All rows from both tables to merge into a single view.

    Not table 1 no criteria:
    MATERIAL     PLANT     QUANTITY     UNIT     POSTING_DATE
    Table 2 criteria:
    MATERIAL     PLANT     SALES_QUANTITY     UNIT     POSTING_DATE     RECORD_TYPE
    Where RECORD_TYPE IN('O','U')
    The view should look like this:
    MATERIAL     PLANT     QUANTITY     UNIT     POSTING_DATE
    I'm not going anywhere, what should I do?

    Hello

    Maybe you want something like this:

    CREATE OR REPLACE VIEW  table_1_and_ou_from_2
    AS
         --
    SELECT     material, plant,       quantity, unit, posting_date
    FROM     table1
         --
    UNION ALL
            --
    SELECT     material, plant, sales_quantity, unit, posting_date
    FROM     table2
    WHERE     record_type     IN ('O', 'U')
    ;
    

    The columns in the view will have the same names that the columns of the 1st branch of the UNION, then the 3rd column will be called quantity, not sales_quantity.

    I hope that answers your question.
    Otherwise, then, as mentioned above, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data (that is, the content of the view).
    Explain, using specific examples, how you get these results from these data.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).
    See the FAQ forum {message identifier: = 9360002}

  • How to join two tables without constraints?

    I want to get back the tables to join without constraints!

    Table 1:

    get 100
    101 wet
    series 102

    and

    Table 2:

    10 xxx movies
    11 yyy
    12 zzz
    QUERY 1:

    SELECT * FROM TO LEAVE P, CUSTES C
    WHERE (P.ID, P.NAME, C.ID, C.NAME) IN (SELECT P1.ID, P1.NAME, C1.ID, C1.NAME
    OF P1, C1 CUSTES TO GO)

    I got the result like this,

    ID ID NAME
    101 wet 10 xxx
    zzz 101 wet 12
    YYY 101 wet 11
    Get 100 10 xxx
    102 the yyy 11 value
    series 102 10 xxx
    100 get 11 yyy
    100 get 12 zzz
    102 the value 12 zzz

    All ranks was sent three times! but I need to write once.

    QUERY 2:

    Select id as pid, name as the name of go
    Union
    Select id as pid, name as the name of custes;

    QUERY2 OUTPUT IS.

    NAME OF PID
    10 xxx movies
    11 yyy
    12 zzz
    get 100
    101 wet
    series 102

    In the 2nd query get the attached files of the two tables. But my need is two tables record to display like this

    ID NAME NAME_1 ID_1
    Get 10 xxx 100
    wet 11 101 yyy
    set of 12 zzz 102

    I'm looking for is, I want to join in parallel columns in my trips!

    Is there a source... ? or the same code is happy...!

    Something like that?

    WITH t1 AS (SELECT '100' id, 'get' name FROM DUAL
                UNION
                SELECT '101', 'wet' FROM DUAL
                UNION
                SELECT '102', 'set' FROM DUAL),
         t2 AS (SELECT '10' id, 'xxx' name FROM DUAL
                UNION
                SELECT '11', 'yyy' FROM DUAL
                UNION
                SELECT '12', 'zzz' FROM DUAL)
    SELECT id,
           name,
           (SELECT id
              FROM (SELECT ID, name, ROWNUM rw FROM t2) t_in
             WHERE t_in.rw = t_out.rw)
              id_1,
           (SELECT name
              FROM (SELECT ID, name, ROWNUM rw FROM t2) t_in
             WHERE t_in.rw = t_out.rw)
              name_1
      FROM (SELECT ID, name, ROWNUM rw FROM t1) t_out
    
    SQL> WITH t1 AS (SELECT '100' id, 'get' name FROM DUAL
      2              UNION
      3              SELECT '101', 'wet' FROM DUAL
      4              UNION
      5              SELECT '102', 'set' FROM DUAL),
      6       t2 AS (SELECT '10' id, 'xxx' name FROM DUAL
      7              UNION
      8              SELECT '11', 'yyy' FROM DUAL
      9              UNION
     10              SELECT '12', 'zzz' FROM DUAL)
     11  SELECT id,
     12         name,
     13         (SELECT id
     14            FROM (SELECT ID, name, ROWNUM rw FROM t2) t_in
     15           WHERE t_in.rw = t_out.rw)
     16            id_1,
     17         (SELECT name
     18            FROM (SELECT ID, name, ROWNUM rw FROM t2) t_in
     19           WHERE t_in.rw = t_out.rw)
     20            name_1
     21    FROM (SELECT ID, name, ROWNUM rw FROM t1) t_out;
    
    ID  NAME ID_1 NAME_1
    --- --- -- ---
    100 get 10 xxx
    101 wet 11 yyy
    102 set 12 zzz
    
    SQL>
    

    The number of rows in the tables are the same?

  • By comparing the two tables for the integrity of the data

    Hi all
    I need to compare two tables for the integrity of the data through the SQL query.

    If you need to compare all the columns of t1 to t2:

    (SELECT * FROM t1
    MINUS
    SELECT * FROM t2)
    UNION ALL
    (SELECT * FROM t2
    MINUS
    SELECT * FROM t1);
    

    Kind regards
    Ankit Rouault
    http://theoraclelog.blogspot.in

  • How to find the data in two tables are the same?

    Hi people,

    Suppose we have table emp01, have 10 records and create another emp02 as table

    create the table emp02 in select * from emp01;

    now both the table has the same data.

    How to find a 'data' in the two tables are the same?

    Hello

    SELECT *
    FROM emp01
    MINUS
    SELECT *
    FROM emp02
    UNION
    SELECT *
    FROM emp02
    MINUS
    SELECT *
    FROM emp01
    

    You can also compare resultset hash to select * in emp01 and select * from emp02 by using dbms_sqlhash.

    Best regards
    Nikolai

  • Came out of two tables with join my best option?

    I have two tables:

    The primary key of the first table is sireID

    She holds the image of several bulls, their name, the owner name and a brief description

    The second table contains the trial of individual dogs scores, joined by the sireID from one table.

    I tried to use a join to the release of information of the dog and it's scores:

    < name cfquery = "getDetailsQuery1" datasource = "#application.database #" >

    Select *.

    Bulls

    order of sireID

    < / cfquery >

    < name cfquery = "getDetailsQuery2" datasource = "#application.database #" >

    Select *.

    of scores

    where sireID = #getDetailsQuery1.sireID #.

    < / cfquery >

    < CFQUERY NAME = 'getDetails' datasource = "#application.database #" >

    SELECT * from getDetailsQuery1

    UNION

    SELECT * from getDetailsQuery2

    ORDER BY sireID

    < / CFQUERY >


    I get an error message:

    [Macromedia] [SequeLink JDBC Driver] [ODBC Socket] [Microsoft] [ODBC Microsoft Access driver] The Microsoft Jet database engine cannot find the input table or query 'getDetailsQuery1 '. Make sure it exists and that its name is spelled correctly.

    Other than to go to mySQL, is there a better way to question it?

    I am trying to create the following

    (First picture)

    Picture of dog

    Owner

    Description

    (Second Table)

    Scoreboard of the trial for this dog

    Next record

    Rick,

    Yes, it would question #2 I've identified.  Now that you have explained what happens in each table, it is clear that a UNION is not the right way to go about it.  You must join.  As I suggested, you want to try to do this in your initial query to the database, and then you won't have to do a query to the query at all.  Here's a Pseudo-requete (since I do not know what are your columns):

    SELECT  sires.sireID               ,sires.picture               ,sires.owner               ,sires.description               ,scores.columnA               ,scores.columnB               ,scores.columnCFROM siresINNER JOIN scores on sires.sireID = scores.sireID
    

    Don't forget, you potentially will answer several recordings for every game of a sireID of the table "bulls" in the corresponding rows in the table "scores" (I assume a one-to-many relationship between the 'bulls' and 'notes').

    -Carl V.

  • How to match columns from two tables with

    Hello:
    I have two tables as below:

    Table1::(Base Table)
    Country | Prefix | Prefix_Length
    Travel | 001 | 3
    CountryB. 0012 | 4
    PaysC | 00443 | 5
    CountryD | 0091 | 4

    :(Detail Table) table2
    The population | Area | Prefix
    500 | AreaA | 0015921
    1000 | AreaB | 00122
    400. AreaC. 00443743
    300. ALIS | 0091333
    100. AreaA | 001

    I need to match these two tables with prefix columns (whose length is not fixed in the two tables: but it starts with 00 in the two tables). Two different countries the prefix may be similar up to a certain length. Thus, Prefix_Length can be used to determine (exactly) how much time should be taken in the search of Table2.

    Output:
    Country | Prefix | Area | Population
    Travel | 001 | AreaA | 600
    CountryB. 0012 | AreaB | 1000
    PaysC | 00443 | AreaC. 400
    CountryD | 0091 | ALIS | 300

    Please help me with your valuable comments.

    -Tender

    Try this

    with base_table as (
                        select 'CountryA' country,'001' prefix,3 prefix_length from dual union all
                        select 'CountryB','0012',4 from dual union all
                        select 'CountryC','00443',5 from dual union all
                        select 'CountryD','0091',4 from dual
                       ),
       detail_table as (
                        select 10 no_of_call,'0015921' prefix from dual union all
                        select 3,'00122' from dual union all
                        select 50,'00443743' from dual union all
                      select 50,'00443643' from dual union all
                        select 300,'0091333' from dual union all
                        select 60,'001' from dual
                       ) 
    
    SELECT  country,
            prefix,sum(no_of_call)
       FROM (
             select  country,
            b.prefix,no_of_call,
            decode(no_of_call,lead(no_of_call,1,0) over(partition by no_of_call order by b.prefix,no_of_call),'y','n') y_or_no
      from  base_table b,
            detail_table d
      where b.prefix = substr(d.prefix,1,prefix_length))
      where y_or_no !='y'
      group by  country,
            prefix
      order by country,
            prefix;
    

    Published by: Vi on 20 February 2012 01:07

  • The most elegant way to get the difference between two tables - not least!

    Hello


    Simplified example of what I'm trying to achieve - I have two tables ORIGINAL and REVISED.


    My aim is to compare the two, such as; -


    When there is data in the two tables I get the difference between the Budget column, and if there is no difference, so I don't want no lines.

    When data exists in the ORIGINAL, but not in review, I want to the inverse of the current value of the Budget column.

    Where the data exist in REVISED I want the REVISED value.

    I can see how I can do this, see below, but is there a more elegant solution?




    Data for the ORIGINAL table
    select '801040' entity, '2186' expense_type, 234000 budget
    from dual
    union all
    select '801040' entity, '3001' expense_type, 1000 budget
    from dual
    union all
    select '801040' entity, 'P132' expense_type, 34000 budget
    from dual
    union all
    select '801040' entity, 'P135' expense_type, 43000 budget
    from dual
    Data for the REVISED table
    select '801040' entity, '2186' expense_type, 235000 budget
    from dual
    union all
    select '801040' entity, 'P132' expense_type, 34000 budget
    from dual
    union all
    select '801040' entity, 'P139' expense_type, 56000 budget
    from dual
    Desired output



    ENTITY EXPENSE_TYPE DIFFERENCE
    ------ ------------ ----------
    801040 2186 1000
    801040 3001-1000
    801040 P135-43000
    801040 P139 56000

    5 selected lines.



    Code current to achieve this, is there a better way?
    select original.entity
    ,      original.expense_type
    ,       (nvl(revised.budget,0) - original.budget) as difference
    from   original
    ,      revised
    where  original.entity = revised.entity(+)
    and    original.expense_type = revised.expense_type(+)
    and   (nvl(revised.budget,0) - original.budget) != 0
    union all
    select  revised.entity
    ,       revised.expense_type
    ,       revised.budget as difference
    from   revised
    where  not exists
    (select 'x'
    from   original
    where  original.entity = revised.entity
    and    original.expense_type = revised.expense_type)
    and    revised.budget != 0
    Thanks a lot for your comments,.


    Robert.

    Published by: Robert Angel on January 17, 2012 03:31 to change is not equal to! = - Thanks for the heads up
    SQL> with original
      2  as
      3  (
      4    select '801040' entity, '2186' expense_type, 234000 budget
      5    from dual
      6    union all
      7    select '801040' entity, '3001' expense_type, 1000 budget
      8    from dual
      9    union all
     10    select '801040' entity, 'P132' expense_type, 34000 budget
     11    from dual
     12    union all
     13    select '801040' entity, 'P135' expense_type, 43000 budget
     14    from dual
     15  )
     16  , revised
     17  as
     18  (
     19    select '801040' entity, '2186' expense_type, 235000 budget
     20    from dual
     21    union all
     22    select '801040' entity, 'P132' expense_type, 34000 budget
     23    from dual
     24    union all
     25    select '801040' entity, 'P139' expense_type, 56000 budget
     26    from dual
     27  )
     28  select *
     29    from (
     30          select nvl(o.entity, r.entity) entity,
     31                 nvl(o.expense_type, r.expense_type) expense_type,
     32                 nvl(r.budget,0) - nvl(o.budget,0) budget
     33            from original o
     34            full join revised r
     35              on o.entity = r.entity
     36             and o.expense_type = r.expense_type
     37         )
     38   where budget <> 0
     39  /
    
    ENTITY EXPE     BUDGET
    ------ ---- ----------
    801040 2186       1000
    801040 P135     -43000
    801040 3001      -1000
    801040 P139      56000
    
    SQL>
    
  • BUG? Statement of BRIDGE to compare two tables

    Hello

    I tried to compare two tables different dbs and remembered a post on applications to Connectin Cross
    http://barrymcgillin.blogspot.com/2010/11/cross-connection-queries.html
    BRIDGE temparb AS "EB05 01"
    (SELECT * FROM arb)
    (SELECT * FROM temparb 
    MINUS 
    SELECT * FROM arb
    )
    UNION ALL
    (SELECT * FROM arb
    MINUS
    SELECT * FROM temparb 
    );
    I expect to get the differences between the ARB table in my current schema and the table alias temparb arb in the other db. Yet it seems that in this case only the table in my current schema is read.

    I materialize at the table BRIDGE with
    BRIDGE temparb AS "EB05 01"
    (SELECT * FROM arb)
    and select the value that I know to be only in the remote schema
    SELECT id FROM temparb WHERE id = 2562;
    SELECT id FROM arb WHERE id = 2562;
    Both times I get no results. I even tried to use aliases on the table, same result. Tested in EA3 3.0 and 3.1.

    Has anyone tried this before?

    Concerning
    Marcus

    Hi Marcus,

    Have you tried without the quotes around the name of connection?

    I have a named connection
    system_local
    which I run the following command

    drop table testbridge_remote;
    drop table testdbrige;
    create table testbridge (col1 int);
    insert into testbridge values (1);
    insert into testbridge values (2);
    insert into testbridge values (3);
    commit;

    I have a named connection
    Name of the connection with space
    I performed the following in the

    drop table testdbrige;
    create table testbridge (col1 int);
    insert into testbridge values (4);
    insert into testbridge values (5);
    insert into testbridge values (3);
    commit;

    Then I can execute the following statement in the connection of system_local/worksheet

    BRIDGE testbridge_remote as the name of connection with Space(select * from testbridge)
    + (SELECT * FROM testbridge_remote +)
    LESS
    SELECT * from testbridge
    +)+
    UNION ALL
    + (SELECT * FROM testbridge +)
    LESS
    SELECT * from testbridge_remote
    +);+

    It works well.
    Returns the rows in the remote table not in the local table and lines in the local table, not the remote table.

    Kind regards
    Dermot
    SQL development team.

  • I have two tables display

    I have two table display and it works very well, but I want to display the data as all first it display data from a table in ascending order, then
    It show data from the second table in the order of asceding

    create or replace view wg_team
    as
    Select emp_name form wgteam
    Union
    Select the name of the component in component_master;

    create or replace view wg_team
    as
    Select emp_name, 1 test for form wgteam
    Union
    Select the name of the component, 2 test of component_master
    order of test;

Maybe you are looking for