Join query with union

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

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

Similar Messages

  • Query with union

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

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

  • Select query with UNION clause in database adapter

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

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

  • Left join query with join of three tables

    I'm trying to build a query which has me stumped. Most of the query is fairly straightforward but I've run into an issue I'm not sure how to solve.
    Background:
    We have actions stored in i_action.
    We have the available attributes for each type of action. The available attributes for each action are described in shared_action_attribute. Each type of action may have up to three attributes or none at all.
    We have the values stored for the attributes in i_attribute_value.
    A written example:
    We have a transfer action (action_code B4). The entry of the B4 action into i_action records the fact that the transfer occurred and the date on which it occurred. The available attributes for a transfer action are the receiving function code, the receiving unit number, and the transfer reason code. These available attribute types and their order are stored in shared_action_attribute. The actual values of the attributes for a specific transfer action are stored in i_attribute_value.
    Now i_action and i_attribute_value can be directly linked through action_seq in i_action and ia_action_seq in i_attribute_value. A left join built between these two tables provides results for all actions (including actions which have no attributes) and attribute values (see query 1 below).
    There are two issues. First, I only want the first two attributes. In order to specify the first two attributes, I also have to link i_attribute_value to shared_action_attribute (which is where the order is stored). I can build a simple query (without the left join) linking the three tables but then actions with no attributes would be excluded from my result set (see query 2 below).
    The second issue is that I would actually like one row returned for each action with first_attribute and second_attribute as columns instead of two rows.
    The final query will be used to create a materialized view.
    Here are the tables and examples of what's stored in them:
    TABLE i_action
    Name Type
    ACTION_SEQ NUMBER(10)
    ACTION_DATE DATE
    ACTION_CODE VARCHAR2(3)
    DELETED VARCHAR2(1)
    EXAMPLE ROWS
    ACTION_SEQ ACTION_DATE ACTION_CODE DELETED
    45765668 09-OCT-09 B2 A
    45765670 09-OCT-09 BA A
    45765672 09-OCT-09 B6 A
    45765673 09-OCT-09 B4 A
    45765674 09-OCT-09 G1 A
    45765675 09-OCT-09 M3 A
    TABLE i_attribute_value
    Name Type
    IA_ACTION_SEQ NUMBER(10)
    SACTATT_SACT_CODE VARCHAR2(3)
    SACTATT_SAT_TYPE VARCHAR2(3)
    VALUE VARCHAR2(50)
    EXAMPLE ROWS
    IA_ACTION_SEQ SACTATT_SACT_CODE SACTATT_SAT_TYPE VALUE
    45765668 B2 ACO 37B
    45765670 BA ROA D
    45765670 BA ROR P
    45765672 B6 CAT C
    45765673 B4 RFC E
    45765673 B4 TRC P
    45765673 B4 RUN 7
    45765674 G1 SS 23567
    45765674 G1 ASG W
    TABLE shared_action_attribute
    Name Type
    SACT_CODE VARCHAR2(3)
    SAT_TYPE VARCHAR2(3)
    ORDER NUMBER(2)
    TITLE VARCHAR2(60)
    EXAMPLE ROWS
    SACT_CODE SAT_TYPE ORDER TITLE
    B2 ACO 1 Office code
    BA ROR 1 Reason for reopen
    BA ROA 2 Reopen authority
    B6 CAT 1 Category
    B4 RFC 1 Receiving function code
    B4 RUN 2 Receiving unit code
    B4 TRC 3 Transfer reason code
    G1 SS 1 Staff sequence
    G1 ASG 2 Assignment reason
    QUERY 1:
    This is my current query along with its results. Most of it is straightforward select but one column is populated using the last_value analytical function (thanks to you guys). The last column in the below view stores the attribute value. What I want is to replace that single column with two columns named first_attribute and second_attribute and to eliminate any other attributes.
    SELECT ia.action_seq, ia.action_date, ia.action_code cod,
    NVL
    (LAST_VALUE (CASE
    WHEN ia.action_code = 'G1'
    AND iav.sactatt_sat_type = 'SS'
    THEN VALUE
    WHEN ia.action_code IN ('A0', 'A1')
    THEN '67089'
    END IGNORE NULLS
    ) OVER (PARTITION BY ia.ici_charge_inquiry_seq ORDER BY ia.action_date,
    ia.serial_number, ia.action_seq),
    '67089'
    ) staff_seq,
    value
    FROM i_action ia LEFT JOIN i_attribute_value iav
    ON iav.ia_action_seq = ia.action_seq
    WHERE ia.deleted = 'A';
    ACTION_SEQ ACTION_DA COD STAFF_SEQ VALUE
    45765668 09-OCT-09 B2 67089 37B
    45765670 09-OCT-09 BA 67089 D
    45765670 09-OCT-09 BA 67089 P
    45765672 09-OCT-09 B6 67089 C
    45765673 09-OCT-09 B4 67089 E
    45765673 09-OCT-09 B4 67089 P
    45765673 09-OCT-09 B4 67089 7
    45765674 09-OCT-09 G1 23567 23567
    45765674 09-OCT-09 G1 23567 W
    45765675 09-OCT-09 M3 23567
    QUERY 2:
    This query limits to the first two attributes but it also drops actions which have no attributes and it still creates multiple rows for each action instead of a single row with two columns for the attributes.
    SELECT ia.action_seq, ia.action_date, ia.action_code cod,
    NVL
    (LAST_VALUE (CASE
    WHEN ia.action_code = 'G1'
    AND iav.sactatt_sat_type = 'SS'
    THEN VALUE
    WHEN ia.action_code IN ('A0', 'A1')
    THEN '67089'
    END IGNORE NULLS
    ) OVER (PARTITION BY ia.ici_charge_inquiry_seq ORDER BY ia.action_date,
    ia.serial_number, ia.action_seq),
    '67089'
    ) staff_seq,
    value
    FROM shared_action_attribute saa, ims_action ia, ims_attribute_value iav
    WHERE iav.ia_action_seq = ia.action_seq
    AND iav.sactatt_sact_code = saa.sact_code
    AND iav.sactatt_sat_type = saa.sat_type
    AND saa.display_order IN ('1','2')
    AND ia.deleted = 'A';
    ACTION_SEQ ACTION_DA COD VALUE
    45765668 09-OCT-09 B2 67089 37B
    45765670 09-OCT-09 BA 67089 D
    45765670 09-OCT-09 BA 67089 P
    45765672 09-OCT-09 B6 67089 C
    45765673 09-OCT-09 B4 67089 E
    45765673 09-OCT-09 B4 67089 7
    45765674 09-OCT-09 G1 23567 23567
    45765674 09-OCT-09 G1 23567 W
    I found this pretty complex to try to write out - I hope I've been clear.
    Thanks so much!

    Ok, here's more information with a simplified question. I figured out the syntax for building my query with the three tables. My final query returns multiple rows (multiple attributes per action). Instead of multiple rows, I'd like two columns (first_attribute, and second_attribute) in a single row (I only need the first two attributes).
    Here's the action table:
    CREATE TABLE I_ACTION
      ACTION_SEQ               NUMBER(10)           NOT NULL,
      ACTION_DATE              DATE,
      ACTION_CODE              VARCHAR2(3 BYTE)     NOT NULL,
      DELETED                  VARCHAR2(1 BYTE),
    );With the following rows added:
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765668, '09-oct-2009', 'B2', 'A');
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765670, '09-oct-2009', 'BA', 'A');
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765672, '09-oct-2009', 'B6', 'A');
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765673, '09-oct-2009', 'B4', 'A');
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765674, '09-oct-2009', 'G1', 'A');
    Insert into I_ACTION (ACTION_SEQ, ACTION_DATE, ACTION_CODE, DELETED)
                  Values (45765675, '09-oct-2009', 'M3', 'A');
    COMMIT;The attribute table is:
    CREATE TABLE I_ATTRIBUTE_VALUE
      IA_ACTION_SEQ          NUMBER(10)             NOT NULL,
      SACTATT_SACT_CODE      VARCHAR2(3 BYTE)       NOT NULL,
      SACTATT_SAT_TYPE       VARCHAR2(3 BYTE)       NOT NULL,
      VALUE                  VARCHAR2(50 BYTE),
    );With the following rows:
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765668, 'B2', 'ACO', '37B');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765670, 'BA', 'ROR', 'P');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765670, 'BA', 'ROA', 'D');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765672, 'B6', 'CAT', 'C');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765673, 'B4', 'RFC', 'E');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765673, 'B4', 'RUN', '7');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765673, 'B4', 'TRC', 'P');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765674, 'G1', 'SS', '23567');
    Insert into I_ATTRIBUTE_VALUE (IA_ACTION_SEQ, SACTATT_SACT_CODE, SACTATT_SAT_TYPE, VALUE)
                           Values (45765674, 'G1', 'ASG', 'W');
    COMMIT;And finally, the shared table:
    CREATE TABLE SHARED_ACTION_ATTRIBUTE
      SACT_CODE      VARCHAR2(3 BYTE)               NOT NULL,
      SAT_TYPE       VARCHAR2(3 BYTE)               NOT NULL,
      TITLE          VARCHAR2(25 BYTE)              NOT NULL,
      DISPLAY_ORDER  NUMBER(2)                      NOT NULL
    );With the following rows:
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B4', 'RFC', 'Y', 'Rcv. Function Code', 1);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B6', 'CAT', 'Y', 'Category', 1);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('G1', 'SS', 'Y', 'Staff Name', 1);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B2', 'ACO', 'Y', '"Other" Office Code', 1);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B4', 'RUN', 'Y', 'Receiving Unit Number', 2);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B6', 'LEP', 'N', 'LEP Issue/Sub Category', 2);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B4', 'TRC', 'Y', 'Transfer Reason Code', 3);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B6', 'NEP', 'N', 'NEP Issue', 3);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('G1', 'ASG', 'Y', 'Assignment Reason', 2);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('B2', 'MSN', 'S', 'Machine Serial Number', 3);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('BA', 'ROR', 'Y', 'Reopen Reason', 1);
    Insert into SHARED_ACTION_ATTRIBUTE (SACT_CODE, SAT_TYPE, TITLE, DISPLAY_ORDER)
                                 Values ('BA', 'ROA', 'Y', 'Reopen Authority', 2);
    COMMIT;Now, this is my current query (it's changed from my first post):
    SELECT ia.action_seq, ia.ici_charge_inquiry_seq, ia.action_date,
           ia.serial_number, ia.reporting_office, ia.reporting_function,
           ia.reporting_unit, ia.action_code, ia.machine_serial_number,
           NVL
              (LAST_VALUE (CASE
                              WHEN ia.action_code = 'G1'
                                 THEN VALUE
                              WHEN ia.action_code IN ('A0', 'A1')
                                 THEN '67089'
                           END IGNORE NULLS
                          ) OVER (PARTITION BY ia.ici_charge_inquiry_seq ORDER BY ia.action_date,
                ia.serial_number, ia.action_seq),
               '67089'
              ) staff_seq,
           (CASE
              WHEN display_order = '1'
              THEN VALUE
           END) first_attribute,
           (CASE
              WHEN display_order = '2'
              THEN VALUE
           END) second_attribute
      FROM ims_action ia
      LEFT JOIN ims_attribute_value iav
           ON iav.ia_action_seq = ia.action_seq
      LEFT JOIN shared_action_attribute
           ON sactatt_sact_code = sact_code
         AND sactatt_sat_type = sat_type
    WHERE ia.deleted = 'A';Which gives me the following results:
    ACTION_SEQ ACTION_DA ACT STAFF_SEQ FIRST_ATTRIBUTE SECOND_ATTRIBUTE   
      45765668 09-OCT-09 B2  67089     37B                                
      45765670 09-OCT-09 BA  67089                     D                  
      45765670 09-OCT-09 BA  67089     P                                  
      45765672 09-OCT-09 B6  67089     C                                  
      45765673 09-OCT-09 B4  67089     E                                  
      45765673 09-OCT-09 B4  67089                     7                  
      45765673 09-OCT-09 B4  67089                                        
      45765674 09-OCT-09 G1  23567                     W                  
      45765674 09-OCT-09 G1  23567     23567                              
      45765675 09-OCT-09 M3  23567                                       The result I WANT is similar but I want the two separate attribute columns on one row as such:
    ACTION_SEQ ACTION_DA ACT STAFF_SEQ FIRST_ATTRIBUTE SECOND_ATTRIBUTE   
      45765668 09-OCT-09 B2  67089     37B                                
      45765670 09-OCT-09 BA  67089     P               D                  
      45765672 09-OCT-09 B6  67089     C                                  
      45765673 09-OCT-09 B4  67089     E               7                  
      45765674 09-OCT-09 G1  23567     23567           W                  
      45765675 09-OCT-09 M3  23567                          Thanks so much!

  • Join query with cfoutput group

    Hi,
    I have the problem with join query and output the goup by, can anyone helps please?
    1: worked fine
    <cfquery name="qOrgs" datasource="#GetDSN()#">
    select org
    from cat
    </cfquery>
    <cfoutput query="qOrgs" group="org">
    #org#<br />
    </cfoutput>
    2: not work, not grouping and display duplicated.
    <cfquery name="qOrgs" datasource="#GetDSN()#">
    select org
      from user u
      inner JOIN cat c
      on u.userid= c.userid
    </cfquery>
    <cfoutput query="qOrgs" group="org">
    #org#<br />
    </cfoutput>
    Thanks

    To expand on Dan's answer;
    The "group" property of the <cfoutput...> tag is fairly simplistic.  All it actuall does is conditionally output content whenever the value of the specified group column changes.
    So if you do not use an ORDER BY clause in your SQL to group the values of that column together (or they don't naturally group because of the current state of the data in the database) then the output is probably not going to be as desired.

  • Table joining query with dates

    Hello,
    I have two tables, one containing a list of pupils, their class start date and their class end dates. The second table contains the address (postcode) history of the pupil. I need to bring these two tables together into one query to show all the classes from the 'pupils' table and add in the most recent postcode of the address prior to the start date of the clasS from the 'addresses' table.  I.e. to show where the pupil lived when they started the class.
    Could anyone show me how to do this please? I've provided a 'desired' table to show what I need the results to look like.
    I hope this is clear, please ask if it isn't - I'm sure this should be simple! But I can't get my head around it. I'm using Oracle 9i and SQL Developer.
    Thanks :)
    select pupils.* from
    (select 'A227' as pupil_id, to_date('21/04/2010','dd/mm/rrrr') class_start, to_date('21/09/2010','dd/mm/rrrr') class_end from dual union all
    select 'A227' as pupil_id, to_date('08/08/2011','dd/mm/rrrr'), to_date('26/10/2011','dd/mm/rrrr') from dual ) pupils
    select addresses.* from
    (select 'A227' as pupil_id, to_date('20/04/2010','dd/mm/rrrr') address_start, null address_end, 'TW1 XVT' as postcode from dual union all
    select 'A227' as pupil_id, to_date('03/08/2011','dd/mm/rrrr'), to_date('31/10/2011','dd/mm/rrrr'), 'TW3 ZE3' as postcode from dual union all
    select 'A227' as pupil_id, to_date('01/11/2011','dd/mm/rrrr'), null, 'n23 4ty' as postcode from dual) addresses
    select desired.* from
    (select 'A227' as pupil_id, to_date('21/04/2010','dd/mm/rrrr') class_start, to_date('21/09/2010','dd/mm/rrrr') class_end, to_date('20/04/2011','dd/mm/rrrr') as address_start, 'TW1 XVT' as postcode from dual union all
    select 'A227' as pupil_id, to_date('08/08/2011','dd/mm/rrrr') class_start, to_date('26/10/2011','dd/mm/rrrr') class_end, to_date('03/08/2011','dd/mm/rrrr') as  address_start, 'TW3 ZE3' as postcode from dual) desired
     

    Hi,
    That's an example of a Top-N Query , and here's one way to do it:
    WITH     got_r_num     AS
         SELECT     p.*
         ,     a.address_start
         ,     a.postcode
         ,     ROW_NUMBER () OVER ( PARTITION BY  p.pupil_id
                                   ,                    p.class_start
                                   ORDER BY          a.address_start     DESC
                           )         AS r_num
         FROM     pupils        p
         JOIN     addresses  a  ON   p.pupil_id        = a.pupil_id
                         AND  p.class_start  >= a.address_start
    SELECT     pupil_id, class_start, class_end, address_start, postcode
    FROM     got_r_num
    WHERE     r_num     = 1
    ;As you can see, the sib-query pairs each pupil and class with all the earlier addresses, and then numbers the addresses such that the latest address_start gets the lowest r_num (that is, 1, since ROW_NUMBER always starts with 1). The main query displays only the most recent address (the one with r_num = 1) for each pupil and class.
    Does addresses.address_end plays any role in this problem? For example, would it matter if the most recent address for a given pupil and class had expired before the class started? That is, if we change the middle of address to:
    select  'A227'          as pupil_id
    ,      to_date ('03/08/2011', 'dd/mm/rrrr')
    ,      to_date ('07/08/2011', 'dd/mm/rrrr')     -- Not 31/10/2011
    ,      'TW3 ZE3'      as postcode
    from      dual would it still count as the last address before the class that started on 08/08/2011?
    Edited by: Frank Kulash on Nov 1, 2011 11:36 AM
    Added question about address_end

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

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

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

  • Can we put Join Query with Expdp in Query Parameter

    Dear All
    Can we put join condition like
    A.roll_no=B.roll_no
    in query clause of EXPDP utility of Oracle 10g.
    My parameter file is
    INCLUDE=TABLE:"IN ('A','B')"
    QUERY ="where roll_no between 1 and 100 "
    then it works and export the data. But I want Above condition.
    But when I specify it gives ORA-39001 error.
    If possible, How and Can you specify example.
    Regards
    Nikhil

    Hello,
    Please check if OSS note 858458 is applicable in your case.
    Regards,
    Praveen

  • Query with UNION option

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

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

  • Joining query with single row result

    dear all,
    i have two tables
    create table item(item_id number primary key,
    item_desc varchar2(200));
    create table item_properties(item_id number references item(item_id),
    property_name varchar2(20),
    property_value varchar2(100));i insert the following records
    insert into items values(1,'CPU');
    insert into item_properties values(1,'RAM','2gb');
    insert into item_properties values(1,'PROCESSOR','2ghz');
    insert into item_properties values(1,'HARDDISK','2ghz');
    commit;now i want a query which produce the following results
    item_id      RAM      PROCESSOR         HARDDISK
    1              2gb        2ghz              2TBHow to generate this result?
    i have create a query but it generate multiple rows, instead i need in a single row like above.
    select i.item_id,p.property_value from items i , item_properties p
    where i.item_id=p.item_id and i.item_id=1;Kind thanks.
    Edited by: Maahjoor on May 7, 2013 12:22 AM

    select i.item_id,
           max(decode(p.property_name,'RAM',p.property_value)) ram,
           max(decode(p.property_name,'PROCESSOR',p.property_value)) processor,
           max(decode(p.property_name,'HARDDISK',p.property_value)) hd
    from items i , item_properties p
    where i.item_id=p.item_id
    and i.item_id=1
    group by i.item_id;Or pivot in 11g
    with details as
    select i.item_id,p.property_name,p.property_value
    from item i , item_properties p
    where i.item_id=p.item_id
    and i.item_id=1
    select *
    from details
    pivot
       max(property_value) for property_name in ('RAM','PROCESSOR','HARDISK')
    );Edited by: jeneesh on May 7, 2013 1:04 PM

  • Join query with inheritance hierarchies

    Hi,
    I need to execute a query that crosses two different inheritance hierarchies:
    1st hierarchy:
    class A {
    private CA myCA;
    class B extends A { ... }
    class C extends A { ... }
    2nd hierarchy:
    class CA { ... }
    class CB extends CA { ... }
    class CC extends CA {
    int i;
    As you can see, these two hierarchies are connected at the level of their base classes: an A object is associated to a CA object.
    The code in these classes makes sure that a B is always associated to a CB object, and that a C object is always associated to a CC object--i.e. a C object will never be associated to a CA or CB object.
    The query I need to execute is the following:
    read all C objects for which the i field of the associated CC object is equal to 42.
    In JDO I'd say something like this:
    "((CC) myCA).i == 42".
    How can I do this in TopLink?
    Thank you,
    Oliver Kamps

    Hi Oliver
    I worked out a few examples to clarify things a bit.
    If the CC Descriptor uses the same table as the CA Descriptor, then you can use a DirectQueryKey on your CA Descriptor:
         public static void amend(Descriptor desc)
              DirectQueryKey key = new DirectQueryKey();
              key.setName("CC_cast_key");
              key.setFieldName("I");
              desc.addQueryKey(key);
    After that, the Expression will become:
    query.setSelectionCriteria(query.getExpressionBuilder().get("ca").get("CC_cast_key").equal(42));
    If the CC info is in a different table then I just used the following Expression:
    query.setSelectionCriteria(query.getExpressionBuilder().get("ca").getTable("CC").getField("CC.I").equal(42));
    The problem in the latter case is that TopLink needs to make an additional JOIN expression (above and beyond what it would've needed to do if you selecting information available in only the CA descriptor).
    JIM

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

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

    dont post duplicate post

  • Inner join query used with 7 Database tables

    HI All,
    In a report they used the Inner join Query with 6 Data base table..now there is a performance issue with at query.
    its taking so much of time to trigger that query. Please help how to avoid that performance issue for that.
    In that 2 database tables containing lakhs of records..
    According to my knowledge it can be avoided by using secondary indexs for those 2 database tables..
    and by replacing the Inner join Query with FOR ALL ENTRIES statement.
    i want how to use the logic by using FORALL ENTRIES statement for this..
    So, please give you proper suggestion to avoid this issue..
    Thanking you.
    Moderator message: Please Read before Posting in the Performance and Tuning Forum
    Edited by: Thomas Zloch on Oct 16, 2011 10:27 PM

    Hi,
    And what do you mean with "they used"? If "SAP used" then yo will need to ask a SAP for note
    FOR ALL ENTRIES is quite good described in help. Please search forum also.
    Without query it won't be possible to tell how it can be optimized, however you can try to use SE30/SAT and ST05. Maybe it will help you.
    BR
    Marcin Cholewczuk

  • Problems with Union statement

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

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

  • MV Incremental Refresh on join query of remote database tables

    Hi,
    I am trying to create a MV with incremental refresh option on a join query with 2 tables of remote database.
    Created MV logs on 2 tables in the remote database.
    DROP MATERIALIZED VIEW LOG ON emp;
    CREATE MATERIALIZED VIEW LOG ON emp WITH ROWID;
    DROP MATERIALIZED VIEW LOG ON dept;
    CREATE MATERIALIZED VIEW LOG ON dept WITH ROWID;
    Now, trying to create the MV,
    CREATE MATERIALIZED VIEW mv_emp_dept
    BUILD IMMEDIATE
    REFRESH FAST
    START WITH SYSDATE
    NEXT SYSDATE1/(24*15)+
    WITH PRIMARY KEY
    AS
    SELECT e.ename, e.job, d.dname FROM emp@remote_db e,dept@remote_db d
    WHERE e.deptno=d.deptno
    AND e.sal>800;
    Getting ORA-12052 error.
    Can you please help me.
    Thanks,
    Anjan

    Primary Key is on EMPNO for EMP table and DEPTNO for DEPT table.
    Actually, I have been asked to do an feasibility test whether incremental refresh can be performed on MV with join query of 2 remote database tables.
    I've tried with all combinations of ROWID and PRIMARY KEY, but getting different errors. From different links, I found that it's possible, but cannot create any successful testcase anyway.
    It will be very much helpful if you can correct my example or tell me the restrictions in this case.
    Thanks,
    Anjan

Maybe you are looking for

  • MacBook pro, snow leopard, iPhone and Bluetooth PAN problems

    Short Version: I am having connectivity issues connecting my new MacBook Pro (MBP) to the Internet thru a Bluetooth PAN on my iPhone4. This has been working fine for a few days and then today it just stopped working responding that my iphone connecti

  • Button Symbol and text change.

    Hi, I have a button symbol with an Instance name of myButton that has a text field in it. The text field is dynamic and an Instance name of myText. I'm trying to change the text from a class I've created like so... myButton.myText.text = "Changed Tex

  • Error Page for PL/SQL module

    I would like to display static html eror page if Oracle Database returns error (for example cannot find procedure). In OAS it can be found in Applications -> Cartridges -> PL/SQL Prameters -> HTML error page. Where have I configure it in IAS? TIA, Gi

  • Iphone 4.0 lost contacts and calendar when synching

    Hi. I did my usual synch with my iphone 4.0 and itunes via USB last night and hey presto all my contacts and my calendar have disappeared from my iphone. My itunes is set up to synch with my outlook account and has been happily doing the same thing f

  • Windows Vista and PC-Suit

    Can t use Connect to the Internet function. No modem found! Help me please.