Alternative to the union in Oracle 10 g

Here's the sql code using the union that I currently use. There are 64 columns of 3 different tables. Out of all the columns that a single column value is a constant for 2 discs.

Select col1, col2, col3,..., 'TO' medium_val
of table1, table2, table3
where table1.col1 = table2.col1
and table2.col1 = table3.col1
and other criteria
Union
Select col1, col2, col3,..., 'GT' medium_val
of table1, table2, table3
where table1.col1 = table2.col1
and table2.col1 = table3.col1
and other criteria

Basically repeating the same code just for the value of a column. Is there any alternative to the union in order to avoid the questioning of two times the same tables.

Hello

Here's one way that will work in Oracle 9 (or higher):

WITH     all_medium_vals     AS
(
     SELECT  'DE' AS medium_val     FROM dual     UNION ALL
     SELECT     'WG'                FROM dual
)
SELECT  col1, col2, col3, ....
,      m.medium_val
FROM      table1
,      table2
,      table3
,     all_medium_vals  m
WHERE      table1.col1      = table2.col1
AND     table2.col1      = table3.col1
AND     ...
;

Do not refer to m.medium_val in the WHERE clause. Leaving him out, you'll do a cross join of your result set of table1, table2, and table3 with 2 lines in all_medium_vals.

Starting Oracle 11, there are also SOME... UNPIVOT.

You will notice that this query is still a UNION, but it's just the UNION of two lines and then by doing the complicated only one query. That will be much more effective that make the complex query twice, and then make a UNION of however many lines that happened to produce. There are ways to generate all_medium_vals without a UNION, but they are not as easy to understand or to debug and are not significantly more effective.

Tags: Database

Similar Questions

  • 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

  • Oracle dynamic SQL with the UNION operator

    Hello

    I have sql string contains two queries separated by the UNION operator. When I run by using DBMS_SQL, it becomes only the first SQL before the UNION operator.

    Can anyone correct how can I Sue UNION in DBMS_SQL?

    Thank you

    Sorry,

    I tested my code

                        BEGIN
                          FOR I IN (SELECT 1 A FROM DUAL UNION
                                  SELECT 2 A  FROM DUAL UNION
                                  SELECT 3 A  FROM DUAL) LOOP
                          DBMS_OUTPUT.PUT_LINE(I.A);
                         END LOOP;
                        END;
                        
    

    and copy this code and PASE to DBMS_SQL. PARSE (all other cases, "... and insert the INSERT STATEMENT but does not erase DBMS_OUTPUT.")

    Edited by: Quluzade Mr. Mahir on February 28, 2011 12:18

  • Alternative and improvement of the union query?

    Hello

    I have 2 sets of similar data together with a union query.

    Simplified example
    *Table A*
    Name Dept
    Ben    1
    Amy   3
    Ken    3
    
    *Table B*
    Name Dept
    Jim    1
    John  2
    Sue   3
    
    select Name, Dept
    from table A
    UNION
    select Name, Dept
    from table B
    I want to do is to ONLY include data from the second part of the union query, where it is associated with the first part of the union query data.

    For example
    select Name, Dept
    from table A
    UNION
    select Name, Dept
    from table B
    where Dept in (select distinct Dept from A)
    That's why John, who works in the Dept 2 would not be included in the results.

    Is there a better or more effective way to achieve that my example using the subquery in the where clause?

    Thank you very much
    Hazel

    Hi, Hazel,

    What you posted is as good as you can get without changing tables.

    Do not say SEPARATE in the IN-under-query:

    ...
    where   Dept in (  select  Dept
                       from    A
                    )
    

    will get the same results, maybe a little faster.

  • Another way for the UNION

    I'm trying to get this result,

    EQPID EVENTVAL 20120501 20120502 20120503 201204 201205
    -------------------------------------------------------------------------------------------------------------------
    MS - A1 21528 28386 18288 821295 908602 TKINQTY
    MS - A1 21510 28359 18270 820470 907321 TKOUTQTY
    MS - B1 21090 17990 15750 467922 557239 TKINQTY
    MS - B1 21080 17982 15735 467495 556730 TKOUTQTY
    MS - C1 35548 23848 26082 914351 1013655 TKINQTY
    MS - C1 35533 23826 26058 913727 1012888 TKOUTQTY
    MS - D1 22680 17568 15030 514467 494510 TKINQTY
    MS - D1 22660 17565 15021 513955 493857 TKOUTQTY
    MS - E1 18450 20916 20754 429057 490732 TKINQTY
    MS - E1 18436 20912 20739 428564 490188 TKOUTQTY
    MS - F1 13968 14436 14670 440760 517343 TKINQTY
    MS - F1 13949 14409 14657 440364 516870 TKOUTQTY
    MS - G1 21060 21600 25240 573409 460286 TKINQTY
    MS - G1 21057 21595 25237 573317 459888 TKOUTQTY
    MS - H1 16452 19278 21456 510192 545201 TKINQTY
    MS - H1 16452 19278 21456 510174 545174 TKOUTQTY
    170776 164022 157270 4671453 4987568 TKINQTY
    170677 163926 157173 4668066 4982916 TKOUTQTY
    341453 327948 314443 9339519 9970484


    in this table

    CUTOFF_DATE EQPID TKINQTY TKOUTQTY
    -------------------------------------------------------------------------
    20120401 MS - A1 46566 46524
    20120401 MS - B1 13734 13720
    and so on...

    Here is my code,

    SELECT EQPID, «EVENTVAL' AS «EVENTVAL', SUM("20120501") AS '20120501', SUM("20120502") AS '20120502', SUM("20120503") AS '20120503', SUM("201204") "201204" AS, SUM("201205") "201205" AS
    Of
    (
    SELECT EQPID, «EVENTVAL' AS «EVENTVAL', SUM("20120501") AS '20120501', SUM("20120502") AS '20120502', SUM("20120503") AS '20120503', SUM("201204") "201204" AS, SUM("201205") "201205" AS
    Of
    (
    SELECT EQPID,
    CASE
    WHEN EQPID LIKE 'MS-% '.
    THEN "TKINQTY".
    END, LIKE "EVENTVAL."

    CASE
    WHEN CUTOFF_DATE = '20120501'
    THEN SUM (QTY)
    END, LIKE "20120501."

    CASE
    WHEN CUTOFF_DATE = '20120502'
    THEN SUM (QTY)
    END, LIKE "20120502."

    CASE
    WHEN CUTOFF_DATE = '20120503'
    THEN SUM (QTY)
    END, LIKE "20120503."

    CASE
    WHEN CUTOFF_DATE BETWEEN '20120401' AND '20120430'
    THEN SUM (QTY)
    END AS "201204."

    CASE
    WHEN CUTOFF_DATE BETWEEN '20120501' AND '20120530'
    THEN SUM (QTY)
    END AS "201205.

    Of
    (
    SELECT CUTOFF_DATE, EQPID, SUM (TKINQTY) QTY.
    OF DAILY_DATA
    WHERE CUTOFF_DATE BETWEEN '20120401' AND '20120530'
    AND EQPID LIKE 'MS-%'
    CUTOFF_DATE GROUP, EQPID
    ORDER BY CUTOFF_DATE, EQPID
    )
    EQPID GROUP, CUTOFF_DATE
    )
    EVENTVAL GROUP, EQPID
    UNION
    SELECT EQPID, 'EVENTVAL' AS 'EVENTVAL', SUM("20120501") AS '20120501', SUM("20120502") AS '20120502', SUM("20120503") AS "20120503", SUM("201204") AS "201204', SUM("201205") AS"201205 '"
    Of
    (
    SELECT EQPID,
    CASE
    WHEN EQPID LIKE 'MS-% '.
    THEN "TKOUTQTY".
    END, LIKE "EVENTVAL."

    CASE
    WHEN CUTOFF_DATE = '20120501'
    THEN SUM (QTYO)
    END, LIKE "20120501."

    CASE
    WHEN CUTOFF_DATE = '20120502'
    THEN SUM (QTYO)
    END, LIKE "20120502."

    CASE
    WHEN CUTOFF_DATE = '20120503'
    THEN SUM (QTYO)
    END, LIKE "20120503."

    CASE
    WHEN CUTOFF_DATE BETWEEN '20120401' AND '20120430'
    THEN SUM (QTYO)
    END AS "201204."

    CASE
    WHEN CUTOFF_DATE BETWEEN '20120501' AND '20120530'
    THEN SUM (QTYO)
    END AS "201205.

    Of
    (
    SELECT CUTOFF_DATE, EQPID, SUM (TKOUTQTY) QTYO
    OF DAILY_DATA
    WHERE CUTOFF_DATE BETWEEN '20120401' AND '20120530'
    AND EQPID LIKE 'MS-%'
    CUTOFF_DATE GROUP, EQPID
    ORDER BY CUTOFF_DATE, EQPID
    )
    EQPID GROUP, CUTOFF_DATE
    )
    EVENTVAL GROUP, EQPID
    )
    GROUP BY ROLLUP (EVENTVAL, EQPID)
    ORDER OF EQPID, EVENTVAL


    It seems to be so long, and I try to avoid the UNION...
    Is there an alternative way to achieve the same result without the help of the union?

    Thank you

    Estelle =)

    Published by: 1004902 on May 8, 2013 17:52

    Published by: 1004902 on May 8, 2013 17:59

    Hello

    Welcome to the forum!

    1004902 wrote:
    I'm trying to get this result,

    EQPID EVENTVAL 20120501 20120502 20120503 201204 201205
    -------------------------------------------------------------------------------------------------------------------


    MS - A1 21528 28386 18288 821295 908602 TKINQTY
    MS - A1 21510 28359 18270 820470 907321 TKOUTQTY
    ...
    in this table

    CUTOFF_DATE EQPID TKINQTY TKOUTQTY
    -------------------------------------------------------------------------
    20120401 MS - A1 46566 46524
    20120401 MS - B1 13734 13720
    and so on...

    Every time you ave a problem, CREATE TABLE post and instructions INSERT for your sample data and the results you want from this data.
    Explain how you get these results from these data.
    Always say what Oracle version you have (for example, 11.2.0.2.0).
    See the FAQ forum {message identifier: = 9360002}

    It seems that the UNION may be replaced by a cross join, or SELECT... UNPIVOT depending on what version of Oracle you are running.

    Published by: Frank Kulash on May 8, 2013 21:52

    You may want something like this:

    WITH    cntr    AS
    (
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL     <= 2
    )
    ,     got_eventval     AS
    (
         SELECT       d.cutoff_date
         ,        d.eqpid
         ,       CASE
                    WHEN  c.n = 1  THEN  'TKINQTY'
                                       ELSE  'TKOUTQTY'
                END     AS eventval
         ,       CASE
                    WHEN  c.n = 1  THEN  d.tkinqty
                                       ELSE  d.tkoutqty
                END     AS qty
         FROM         daily_data  d
         CROSS JOIN  cntr     c
         WHERE    cutoff_date BETWEEN '20120401'
                         AND     '20120530'
            AND       eqpid           LIKE 'MS-%'
    )
    SELECT       eqpid, eventval
    ,       SUM (CASE WHEN cutoff_date = '20120501'  THEN qty END)  AS "20120501"
    ,       SUM (CASE WHEN cutoff_date = '20120502'  THEN qty END)  AS "20120502"
    ,       SUM (CASE WHEN cutoff_date = '20120503'  THEN qty END)  AS "20120503"
    ,       SUM (CASE WHEN cutoff_date LIKE '201204' THEN qty END)  AS "201204"
    ,       SUM (CASE WHEN cutoff_date LIKE '201205' THEN qty END)  AS "201205"
    FROM       got_eventval
    GROUP BY  eqpid, eventval
    ORDER BY  eqpid, eventval
    ;
    

    Of coursel, I can't test it until you post the sample data.

    Storage of the information on the dates in a column VARCHAR2 is a very bad idea. The columns DATE to the date information.

  • FILL MONTHS DYNAMICALLY IN THE ITEM VIA ORACLE FORMS GROUP RECORDING LIST

    Experts

    I have a query in oracle forms. I need to fill Last_Month, Current_Month & Next_month based on Sys_Date in the list box. I am able to get the values in SQL if I run the query below, but when I use the version of Oracle Forms 10.1.2.0.2, I could not get the values of Mr. can you please guide me where I'm wrong.

    declare

    v_rg_id recordgroup: = NULL;

    Number of V_errorcode;

    v_rg_salmth varchar2 (30): = "MONTHS";

    Begin

    v_rg_id: = find_group (v_rg_salmth);

    If id_null (v_rg_id) then

    v_rg_id: = create_group_from_query (v_rg_salmth,

    "WITH (AS MONTH_COUNTER)

    SELECT LEVEL 2 AS ID

    OF THE DOUBLE

    CONNECT BY LEVEL = 2

    UNION

    SELECT LEVEL AS ID

    OF THE DOUBLE

    CONNECT BY LEVEL = 0

    )

    SELECT TO_CHAR (ADD_MONTHS (TO_CHAR (SYSDATE), ID), MONTH) AS MONTH_COUNTER MONTHS ');

    v_errorcode: = populate_group (v_rg_id);

    If v_errorcode = 0 then

    message ("record group filled with data");

    on the other

    message ("V_errorcode is:" | ") v_errorcode);

    end if;

    End if;

    populate_list('MASTER.) MONTH, v_rg_id);

    end;

    The rather complicated query, try

    SELECT ADD_MONTHS (SYSDATE-1) TO DOUBLE

    UNION ALL

    SELECT SYSDATE DOUBLE

    UNION ALL

    SELECT THE DOUBLE ADD_MONTHS(SYSDATE,1)

    BTW. to use a query to populate a list, you must select two values, a label and a value, check it fill listitems dynamically - Andreas Weiden - on Oracle

  • Select records of first and the last in the Group results - Oracle 11 g

    I have the following information in the Oracle 11 g table:

    Qty.Production orderDate and time
    200000000000000000112 JANUARY 14 00:02
    200000000000000000112 JANUARY 14 00:05
    200000000000000000112 JANUARY 14 00:07
    2000000000000000001JANUARY 13, 14 00:09
    300000000000000000212 JANUARY 14 00:11
    3000000000000000002JANUARY 12/14 00:15
    300000000000000000212 JANUARY 14 00:20
    300000000000000000214 JANUARY 14 00:29

    I here to write a query that returns the following:

    Qty.Production orderFirstLast
    800000000000000000112 JANUARY 14 00:02JANUARY 13, 14 00:09
    1200000000000000000212 JANUARY 14 00:1114 JANUARY 14 00:29

    Is the sum of the Qty. grouping column by Production order and the time of the first and last records for each Production order .

    I came up with a query that gave this result:

    Qty.Production orderFirstLast
    800000000000000000112 JANUARY 14 00:0214 JANUARY 14 00:29
    1200000000000000000212 JANUARY 14 00:0214 JANUARY 14 00:29

    Wwhich means that the and last columns show the first and last date / time the whole table. Please note that there is a shadow table. Sorry, I am now allowed to write the actual query

    , I came with given work policies don't allow me to share. Also, I tried with windowing functions such as rank()and row_number() but my user has not enough privileges to do so.

    Help or advice will be greatly appreciated.

    Due to the fact that Oracle does not have the lines in a particular order, it would be wrong that the first 'date' would be the first line to be processed by the query.

    So, you will need to provide another column if you do not want to look at the table as ordered by date.

    Any analytic function must also allows you to provide the 'to' and if that's the date, then just a simple query:

    SQL > WITH Tab1 (quantity, Production_Order, patch)

    2 ALSO (SELECT 20, '00000000000000001', TO_DATE (12 JANUARY 14 00:02 ', 'DD-MON-YY HH24') FROM DUAL UNION ALL)

    20. SELECT 3, '00000000000000001', TO_DATE (12 JANUARY 14 00:05 ', 'DD-MON-YY HH24') FROM DUAL UNION ALL

    4. SELECT 20, '00000000000000001', TO_DATE (12 JANUARY 14 00:07 ', 'DD-MON-YY HH24') FROM DUAL UNION ALL

    20. SELECT 5, '00000000000000001', TO_DATE (13 JANUARY 14 00:09 ', 'DD-MON-YY HH24') FROM DUAL UNION ALL

    6. SELECT 30, '00000000000000002', TO_DATE (12 JANUARY 14 00:11 ', 'DD-MON-YY HH24') FROM DUAL UNION ALL

    30 SELECT 7, '00000000000000002', TO_DATE (12 JANUARY 14 00:15 ', 'HH24 LUN-JJ-AA') OF ANY UNION DOUBLE

    8. SELECT 30, '00000000000000002', TO_DATE (12 JANUARY 14 00:20 ', 'DD-MON-YY HH24') FROM DUAL UNION ALL

    9 SELECT 30, '00000000000000002', TO_DATE (14 JANUARY 14 00:29 ', 'DD-MON-YY HH24') FROM DUAL)

    10. SELECT SUM (Qty), Production_Order, MIN (update), MAX (updated)

    11 OF Tab1

    GROUP of 12 BY Production_Order

    13 * ORDER BY Production_Order

    SQL > /.

    SUM (QTY) PRODUCTION_ORDER (PDATE) MIN MAX (PDATE)

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

    80 00000000000000001 12 January 2014 00:02:00 January 13, 2014 00:09:00

    120 00000000000000002 12 January 2014 00:11:00 January 14, 2014 00:29:00

  • Diagram of the Avro - optional fields - not give 'in the Union"exception

    Hi all

    I have a problem with the help of the optional fields in a schema by using the JSONAvroBinding and avro.

    If I have a scheme of the user who has a first name field. The user may choose to provide the name or not. The field is optional from the perspective of readers.

    (this comes from http://docs.oracle.com/cd/NOSQL/html/GettingStartedGuide/avroschemas.html who is nosql avro documentation)

    {

    "type': 'record."

    "namespace": "com.example."

    'name': 'Full name',

    'fields':]

    {'name': 'first', 'type': ['string', 'null']},

    {'name': 'last', 'type': "string", "default": "Smith"}

    ]

    }

    Here, if I provide this example as Avro scheme, then omit the name when I write, it will work without problem. But when I try to write 'John' it will fail with:

    Caused by: org.apache.avro.UnresolvedUnionException: not in the union ['string', 'null']: 'John '.

    to oracle.kv.impl.api.avro.JsonBinding$ JsonData.resolveUnion (JsonBinding.java:403)

    at org.apache.avro.generic.GenericDatumWriter.resolveUnion(GenericDatumWriter.java:144)

    to oracle.kv.impl.api.avro.JsonBinding$ JsonDatumWriter.write (JsonBinding.java:599)

    to oracle.kv.impl.api.avro.JsonBinding$ JsonDatumWriter.writeRecord (JsonBinding.java:695)

    to oracle.kv.impl.api.avro.JsonBinding$ JsonDatumWriter.write (JsonBinding.java:565)

    Truncated. check the log file full stacktrace

    It does not work:

    {

    'first': 'John ',.

    "last": "Smith".

    }

    It works:

    {

    "last": "Smith".

    }

    It seems that someone else has had this problem and posted on the Avro site, where a response said that it might be NoSQLs due to lack of libraries:

    "The error is thrown from the Oracle NoSQL Avro library use and is not clearly a problem of Avro. You might be better off trying to get help for this Oracle? »

    https://issues.Apache.org/jira/browse/Avro-1376

    Any thoughts? To me, this seems to be a bug in the JsonBinding class?

    According to this:

    http://www.Oracle.com/technetwork/database/nosqldb/learnmore/nosqldb-FAQ-518364.html#HowtouseAvrounionstoencodeoptionalfields

    Sending

    {

    'first': 'John ',.

    "last": "Smith".

    }

    is not correct.

    Instead, sending:

    {

    'first': {}

    'chain': 'John '.

    },

    "last": "Smith".

    }

    works.

  • Using Clob in the Union query

    Hello everyone, have a look at the statements below and let me know what wrong with my approach.

    CREATE TABLE test
    (column1 CLOB);
    
    
    INSERT INTO test 
    SELECT to_clob('This is test') FROM dual;
    

    When I run the query below, I get data types incompatible error (ORA-00932). Help, please.

    SELECT to_clob('This is test two') FROM dual 
    UNION
    SELECT column1 FROM test;
    

    Kind regards

    Shiva.

    Hey Shiva,

    Copied from documentation

    http://docs.Oracle.com/CD/B19306_01/server.102/b14200/queries004.htm

    Restrictions on operators define the set operators are subject to the following restrictions:

    • Set operators are not valid on columns of type BLOB , CLOB , BFILE , VARRAY , or a nested table.
    • The UNION , INTERSECT , and MINUS operators are not valid on LONG columns.
    • If the selection list prior the operator set contains an expression, you must provide an alias for the expression column to refer in the order_by_clause .
    • You cannot specify also the for_update_clause with the set operators.
    • You cannot specify the order_by_clause in the subquery of these operators.
    • You cannot use these operators in SELECT statements containing the TABLE expressions collection.
  • What is the proposal for substitution for the AMPERSAND (&amp;) in oracle?

    Hi all

    It's my request...

    Select * from
    (
    Select 'abc' a Union double
    Select 'def' a Union double
    Select 'abc and xyz' double
    ) where a = 'abc and xyz '.

    Output must be "abc and xyz."

    Since I use '&' it prompts you for a value between my keyboard.

    Yes, I know, I'll get the output using set scan off the coast, together set off, set escape on.

    What is the proposal for substitution for the AMPERSAND (&) in oracle?

    "Abc and xyz" value will be passed forward and will be passed to the select...

    Kind regards
    Varma Koffi N

    nkvkashyap wrote:
    Hi all

    It's my request...

    Select * from
    (
    Select 'abc' a Union double
    Select 'def' a Union double
    Select 'abc & xyz' double
    ) where a ='abc & xyz';

    Output must be 'abc & xyz '.

    Since I use '&' it prompts you for a value between my keyboard.

    Yes, I know, I'll get the output using set scan off the coast, together set off, set escape on.

    What is the alternate to sign & (and) in oracle?

    The value 'abc & xyz' will be passed forward and will be passed to the select statement.

    Kind regards
    Varma Koffi N

    I think you're confused. Oracle (i.e. DB) doesn't care &. However the frontends like SQL * more: and request you substitution (unless the whole set off etc.).

    If you use Java, and pass channels with & without problem.

  • Need info regarding the behavior of Oracle UCM accounts and security groups

    Need information about the behavior of Oracle UCM accounts and security groups.

    Oracle UCM version: 11.1.1.5.0

    Steps to follow:
    1. log in with the user "weblogic" and content created with the id of "content1".
    2. apply the "@acc1 (R)" and "TestGroup1" to her quote created in step 1
    3. close the session
    4. connect as that of "acc1user1", the user is not able to see the "content1".
    5. close the session
    6 log in as "role1user1", the user is not able to see the "content1".

    Account information and group:
    1. the user "acc1user1" is part of the "@acc1 (R).
    2. the user "role1user1" belongs to 'role1 (R)' and is mapped to "TestGroup1" at the Complutense University of MADRID

    Expected:
    The 'acc1user1' and 'role1user1' should be able to see "content1" because they have at least the read permission.

    Please help me understand why users aren't able to see the content.

    See section 5.5.1.1 in the Manual: http://docs.oracle.com/cd/E23943_01/doc.1111/e10792/c05_security.htm#BGBGIJDJ

    If the accounts and roles are used, the result is an intersection, not a Union.

  • Order by in the Union

    Hi all

    If we do not specify the order of the UNION/UNION ALL clause then what is the default order of data

    Select Stmnt1
    UNION
    Select Stmnt2



    Select Stmnt1
    UNION ALL
    Select Stmnt2

    I use the version 10G AND 11 G

    Thank you

    Hi Smidreb,

    According to the oracle docs (so you can test also),

    You cannot specify the order_by_clause in the subquery of the SET operators (IE in your UNION/UNION ALL cases).

    So if your question

    select col1,col2... from t1 order by col1
    union
    select col1,col2... from t2 order by col1
    

    OR

    select col1,col2... from t1 order by col1
    union ALL
    select col1,col2... from t2 order by col1
    

    The two queries above will give errors. So that something of not authorized by Oracle. If you think a bit you will come to know why it is so.

    But even if you need ordered result, you can achieve as below (drafting of an outer query):

    Select * from (
    select col1,col2... from t1
    union/UNION ALL
    select col1,col2... from t2
    )
     order by col1;
    

    Come to your original question, what will happen if you do not specify order by?
    Now you know u cann't apply the order of. Also, you know how u can apply, if you wish.

    Also without a prescription by Oracle does not guarantee any order--> FACT.

    Now, the answer is obvious.

    Published by: J2EE_Life on January 28, 2012 08:48

    Published by: J2EE_Life on January 28, 2012 08:49

  • How to find the Union between the column in the table

    Hello

    I have the followin table and I want to find the union between the lines as below,
    create table test5 (tidset varchar2(200));
    
    insert into test5 values('1;2;3;4;5;6;7;8;9;10;11;12;13;15;16;17;18;19;20');
    insert into test5 values('1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20');
    insert into test5 values('1;2;3;4;6;7;8;9;11;12;13;15;16;19;20');
    insert into test5 values('1;4;6;10;6;11;12;13;14;15;16;17;18;19;20');
    I need the result to be as below.
    1;4;6;11;12;13;15;16;19;20
    any help please,.

    Published by: user11309581 on November 17, 2011 21:32

    Always mention what version of oracle you are using.

    The following query will give you the desired result oracle version 11g release 2.

    Connected to Oracle Database 11g Release 11.2.0.2.0 
    
    SQL>
    SQL> with test5 as
      2  (
      3  select '1;2;3;4;5;6;7;8;9;10;11;12;13;15;16;17;18;19;20' tidset from dual union all
      4  select '1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20' from dual union all
      5  select '1;2;3;4;6;7;8;9;11;12;13;15;16;19;20' from dual union all
      6  select '1;4;6;10;6;11;12;13;14;15;16;17;18;19;20' from dual
    -- "END OF SAMPLE DATA"
      7  ), all_items as
      8  (
      9    SELECT regexp_substr(t.tidset, '[^;]+', 1, column_value) item
     10          ,t.tidset
     11      FROM test5 t
     12          ,TABLE(CAST(MULTISET
     13                      (SELECT LEVEL
     14                         FROM dual
     15                       CONNECT BY LEVEL <=
     16                                  length(regexp_replace(t.tidset, '[^;]')) + 1) AS
     17                      sys.odcinumberlist))
     18  ),item_counts as
     19  (
     20    SELECT DISTINCT a.item
     21                   ,COUNT(DISTINCT a.tidset) over() total_tidset
     22                   ,COUNT(a.item) over(PARTITION BY a.item) item_occurance_count
     23      FROM (SELECT DISTINCT item
     24                           ,tidset
     25              FROM all_items) a
     26  )
     27  SELECT listagg(item, ';') within GROUP(ORDER BY to_number(item)) common_items
     28    FROM item_counts
     29   WHERE total_tidset = item_occurance_count
     30  ;
    
    COMMON_ITEMS
    --------------------------------------------------------------------------------
    1;4;6;11;12;13;15;16;19;20
    
    SQL> 
    

    OR if you have any version of oracle less than 11g release 2 then replace below mentioned line

    SELECT listagg(item, ';') within GROUP(ORDER BY to_number(item)) common_items
    

    with this one

    select xmlagg(xmlelement(c,item||';').extract('//text()') order by (to_number(item))) common_items
    

    It is because the listagg function used in above query is not supported in versions of oracle 11.2 below.

    I hope this helps.

    -Gregory

    Published by: Mohamed Diarra on November 18, 2011 01:05
    Extra line to be changed for the oracle version lower than 11.2

  • change the view in oracle

    Hello all;

    I have an interesting problem

    I have the following code
    create table table_A 
    (
          ID varchar2(200),
          qty number(6)
    );
    
    insert into table_a
      (id, qty)
    values
      ('A', 10);
    
    insert into table_a
      (id, qty)
    values
      ('B', 6);
    
    create table table_B 
    (
          ID varchar2(200),
          colour varchar2(200)
    );
    insert into table_b
      (id, colour)
    values
      ('A', 'Red');
    insert into table_b
      (id, colour)
    values
      ('B', 'Black');
    
    create table table_info
    (
           ID varchar2(200),
           addr varchar2(1000)
    );
    
    insert into table_info
      (id, addr)
    values
      ('A', '2034 James Street');
    
    insert into table_info
      (id, addr)
    values
      ('B', '2044 Main Street');
    and I use a selection similar to this
    select g.id, g.qty, h.colour from table_A g, table_B h
    where g.id = h.id
    gives me the following display below
    ID     QTY    COLOUR
    A       10      Red
    B         6      Black
    now, I would like to change the display so that I can get this rather
    ID       Addr
    A        '2034 James Street'
    ID       QTY       Colour
    A          10         Red
    ID      Addr
    B       '2044 Main Street'
    ID      QTY        Colour
    B         6           Black
    Any help is appreciated. Thank you.

    Hello

    Here's one way:

    WITH     union_data  AS
    (
         SELECT    g1.id
         ,       TO_CHAR (g1.qty)     AS column_2
         ,       h1.colour
         ,       g1.id               AS sort_id
         ,       2               AS sort_line
         FROM        table_A      g1
         ,       table_B      h1
         WHERE       g1.id            = h1.id
                       --
             UNION ALL
                   --
         SELECT       'ID'               AS id
         ,       'QTY'               AS column_2
         ,       'Colour'          AS colour
         ,       g2.id               AS sort_id
         ,       1               AS sort_line
         FROM        table_A      g2
         ,       table_B      h2
         WHERE       g2.id            = h2.id
                       --
             UNION ALL
                   --
            SELECT    t1.id
         ,       t1.addr          AS column_2
         ,       NULL               AS colour
         ,       t1.id               AS sort_id
         ,       0               AS sort_line
         FROM       table_info   t1
                       --
             UNION ALL
                   --
            SELECT    'ID'               AS id
         ,       'Addr'          AS column_2
         ,       NULL               AS colour
         ,       t2.id               AS sort_id
         ,       -1               AS sort_line
         FROM       table_info   t2
    )
    SELECT       id, column_2, colour
    FROM       union_data
    ORDER BY  sort_id
    ,            sort_line
    ;
    

    Here's how it works:

    You already know how to get the output lines containing the values of the quantity and color.
    You can generate a header line for each of these lines, which owns the labels 'ID', 'Quantity' and 'COLOR', and do a UNION to combine the results.

    SELECT    g.id
    ,       TO_CHAR (g.qty)     AS qty
    ,       h.colour
    FROM        table_A      g
    ,       table_B      h
    WHERE       g.id            = h.id
                       --
        UNION ALL
                   --
    SELECT       'ID'
    ,       'QTY'
    ,       'Colour'
    FROM        table_A      g
    ,       table_B      h
    WHERE       g.id            = h.id
    ;
    

    Output:

    ID         QTY        COLOUR
    ---------- ---------- ----------
    A          10         Red
    B          6          Black
    ID         QTY        Colour
    ID         QTY        Colour
    

    The problem with the above output is the header lines should be intertwined with data lines, firstly a header, and the data accompanying it. To do this, we can add additional columns to each branch of the UNION that are just for sorting:

    SELECT    g1.id
    ,       TO_CHAR (g1.qty)     AS qty
    ,       h1.colour
    ,       g1.id               AS sort_id
    ,       1               AS sort_line
    FROM        table_A      g1
    ,       table_B      h1
    WHERE       g1.id            = h1.id
                       --
        UNION ALL
                   --
    SELECT       'ID'               AS id
    ,       'QTY'               AS qty
    ,       'Colour'          AS colour
    ,       g2.id               AS sort_id
    ,       2               AS sort_line
    FROM        table_A      g2
    ,       table_B      h2
    WHERE       g2.id            = h2.id
                       --
    ORDER BY  sort_id
    ,            sort_line
    ;
    

    Output:

    ID         QTY        COLOUR     SORT_ID     SORT_LINE
    ---------- ---------- ---------- ---------- ----------
    A          10         Red        A                   1
    ID         QTY        Colour     A                   2
    B          6          Black      B                   1
    ID         QTY        Colour     B                   2
    

    Who has solved a problem but created another. We do not want to see the columns sort_id and sort_line; We just want to use in the ORDER BY clause. However, in a UNION you CN can't ORDER BY anything which is not in the SELECT clause. Depending on your front end, you may be able to hide columns (in SQL * Plus, for example, you can say "COLUMN sort_id NOPRINT"), but here's how you can work around the problem in SQL pure, by the UNION in a separate subquery of the ORDER OF:

    WITH     union_data  AS
    (
         SELECT    g1.id
         ,       TO_CHAR (g1.qty)     AS qty
         ,       h1.colour
         ,       g1.id               AS sort_id
         ,       2               AS sort_line
         FROM        table_A      g1
         ,       table_B      h1
         WHERE       g1.id            = h1.id
                       --
             UNION ALL
                   --
         SELECT       'ID'               AS id
         ,       'QTY'               AS qty
         ,       'Colour'          AS colour
         ,       g2.id               AS sort_id
         ,       1               AS sort_line
         FROM        table_A      g2
         ,       table_B      h2
         WHERE       g2.id            = h2.id
    )
    SELECT       id, qty, colour
    FROM       union_data
    ORDER BY  sort_id
    ,            sort_line
    ;
    

    Output:

    ID         QTY        COLOUR
    ---------- ---------- ----------
    ID         QTY        Colour
    A          10         Red
    ID         QTY        Colour
    B          6          Black
    

    It's perfect for the table_a table_b data. To get the data of table_info thus, simply add 2 branches in addition to the UNION, as indicated at the beginning of this message.

    It's a bit pointless to do 2 separate petitions, one to get the data and the other to get the header, when these 2 queries use the same tables and the same WHERE clause. An alternative is to cross join table to another table (or, more likely, a game of operation in the form of table of results), which has the values n = 1 and n = 2 and use CASE expressions to display data or its header. I'll leave that as an exercise for the reader.

    Published by: Frank Kulash, January 18, 2011 13:02
    Explanation added.

  • avoiding the union

    BANNER
    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    PL/SQL Release 10.2.0.4.0 - Production
    CORE     10.2.0.4.0     Production
    TNS for HPUX: Version 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production
    WITH t AS (SELECT 1 id,
                      'A' grp,
                      1 account,
                      'I' TYPE
                 FROM DUAL
               UNION ALL
               SELECT 2,
                      'A',
                      1,
                      NULL
                 FROM DUAL
               UNION ALL
               SELECT 3,
                      'A',
                      2,
                      NULL
                 FROM DUAL
               UNION ALL
               SELECT 4,
                      'B',
                      1,
                      'I'
                 FROM DUAL
               UNION ALL
               SELECT 5,
                      'B',
                      1,
                      NULL
                 FROM DUAL)
    IDGRP   ACCOUNT    TYPE
    1   A 1             I
    2   A     1
    3   A     2
    4    B     1          I
    5    B     1 
    When the account is changed by Group (such as going from account 1, 2) I need an additional line with a kind of R
    I expected results if I use a union
    SELECT ID,
           grp,
           account,
           CASE
              WHEN LAG (account) OVER (PARTITION BY grp ORDER BY id) != account
              THEN
                 'R'
              ELSE
                 TYPE
           END
              TYPE
      FROM t
    UNION 
    SELECT * FROM t;
    ID,GRP,ACCOUNT,TYPE
    1 A 1 I
    2 A 1
    3 A 2 R
    3 A 2,
    4 B 1 I
    5 B 1 
    However, I want to do it without the union
    There is a solution to help connect by clause model or somehting?

    Published by: pollywog on October 29, 2010 11:32

    Published by: pollywog on October 29, 2010 11:38

    Published by: pollywog on October 29, 2010 11:39

    This code will work only, when there is no space in the ID
    This means that every integer between 1,..., n must be there.

    WITH t AS (SELECT 1 id, 'A' grp, 1 account, 'I' TYPE  FROM DUAL UNION ALL
               SELECT 2, 'A', 1, NULL FROM DUAL UNION ALL
               SELECT 3, 'A', 2, NULL FROM DUAL UNION ALL
               SELECT 4, 'B', 1,'I'   FROM DUAL UNION ALL
               SELECT 5, 'B', 1, NULL FROM DUAL)
    --
    --
     select id, grp, account,
              decode(level,1,TYPE,'R')
     from t
     where level in (1,2)
     connect by (prior id=id-1 and prior grp=grp and prior account != account)
     order by id;
    

    result:

    id      grp     acount  type
    -----------------------------
    1     A     1     I
    2     A     1
    3     A     2     R
    3     A     2
    4     B     1     I
    5     B     1     
    

    Published by: hm on 29.10.2010 09:50

Maybe you are looking for

  • Cannot open programs using the icons on the desktop

    Hello I just noticed that I can not access the internet directly by clicking on the icon. a window opens asking for me to choose the program I want to use to open IE.  He would then run the program but nothing happens and it goes in circles asking to

  • The AP client or Repeater? What do I need here?

    Help, please! I have a network D - Link Wi - Fi and in my home that meets all my needs very well.  I'm trying to expand my network to my store about 100 yards away.  I've set up a very robust D-Link access point outside and I see my network in most o

  • Center of media as a dvr

    Can I use media center, wmc, as a digital video recorder if I use a VCR that requires that the tv on channel 3 or 4?  I would like to as wmc to stay on track 4, but also to send an IR signal to my box to change the channel on which my regular program

  • backup windows 7 to windows 8

    Hello world I backed up all the data in my pc HP PAVILION G6-1293 sl with Windows 7 backup program. Now, my laptop got stolen. so I'm ready to buy a new laptop with Windows 8. What happens when I try to restore my data in Windows 8. I have will go th

  • Window printing preferences Photosmart 5520

    The Properties window of the printer for my D5160 late used to look something like this: By comparing the Properties window of the printer (for example, the inside Word on my Vista PC) for my new 5520 new seems very basic: This brings me to wonder if