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.

Similar Messages

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

  • Query with/without group by returning totally different results

    Hi,
    I've got this query:
      select count(DISTINCT UL.user_id)
      from app_user_log UL,
        company_registry CR,
        productarea PA,
        user_registry UR
           where UL.time_stamp >= to_date('04/02/2002','dd-mm-yy') 
           and UL.time_stamp <= to_date('10/02/2002','dd-mm-yy') 
         and UL.description = 'Open'
         and UR.INT_COMPANY_ID=CR.COMPANY_ID
         and PA.PRODUCTAREA_ID=CR.PRODUCTAREA_ID
         and UL.USER_ID=UR.USER_IDIt returns the value 262
    I then want to divide this into one block pr application_id so that I get the number of users with app_id=1 as first applications, the number of users with app_id=2 as first application, and so on..
    I thought this would be it:
      select ul.application_id, count(DISTINCT UL.user_id)
      from app_user_log UL,
        company_registry CR,
        productarea PA,
        user_registry UR
           where UL.time_stamp >= to_date('04/02/2002','dd-mm-yy') 
           and UL.time_stamp <= to_date('10/02/2002','dd-mm-yy') 
         and UL.description = 'Open'
         and UR.INT_COMPANY_ID=CR.COMPANY_ID
         and PA.PRODUCTAREA_ID=CR.PRODUCTAREA_ID
         and UL.USER_ID=UR.USER_ID
          group by ul.application_idBut this query yields an output with the sum of the counts way above 262 as I would expect..

    Approach the problem as you would any programming problem - break it down into simpler problems. Solve each of these logically.
    Simplifying here, but you should get the basics of this. For example. We need the earliest time stamp from a table. Then we need to select a distinct list of users and applications for that time stamp.
    with EARLIEST_TIME_STAMP as (
    select
    MIN( datetime_stamp ) AS datetime
    from my_table
    select
    count( DISTINCT userid ) as DISTINCT_USERS,
    count( DISTICT applications ) as DISTINCT_APPLICACTION
    from my_table t
    join EARLIEST_TIME_STAMP s
    on s.datetime = t.datetime_stamp

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • Ora-03114 running a query with group by

    Hi, I've a query with a group by on a sub-query, something like
    SELECT <40+ fields>
      FROM (SELECT <40+ fields>
              FROM table
    GROUP BY <40+ fields> I don't have any problem running this query directly via toad. This query is a cursor in a procedure in a package and, if I invoke it on the same data, the session crash "with ora-03114 not connected to oracle". If I modify the query selecting less fields, 22, I don't have any problem while with 23 the crash appears.
    Furthermore, I don't have any problem in other databases with similar data.
    The db version is 9.2.0.6.0 - 64bit
    Any idea/advice?
    Edited by: 912104 on 3-feb-2012 2.02
    Edited by: 912104 on 3-feb-2012 2.03
    Edited by: 912104 on 3-feb-2012 2.03

    912104 wrote:
    I have difficult to have more information, I can only add:
    select * from v$version
    BANNER
    Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
    PL/SQL Release 9.2.0.6.0 - Production
    CORE    9.2.0.6.0 ;   Production
    TNS for IBM/AIX RISC System/6000: Version 9.2.0.6.0 - Production
    NLSRTL Version 9.2.0.6.0 - Production Can I ask what do you think about the different behavior via toad and via package? I mean, it's possible that is a server bug?
    Edited by: 912104 on 3-feb-2012 4.02Are you retrieving the entire result set via toad? or just the first few records?
    Are there any TOAD non-fetched column values in your result set, i.e. CLOBS, XMLTypes, nested types?
    What does your code do with the cursor? Presumably you don't loop through it and do nothing. Are you sure it's the select that's causing the error, or something you are doing with the data in the procedure?
    Can you not adding some instrumentation to your code so that you know exactly what line/data values are processed at the time of the crash? Does it always crash on the same values/line or does it vary?
    "Furthermore, I don't have any problem in other databases with similar data." ... what do you mean by "similar data". You only need one odd value in one column to cause you a 3114 in the right circumstances. It seems like you are long way off establishing what exactly the problem is. It's always useful not to close doors to lines of thought. You need to systematically track down the root cause of the issue by exclusion and assumptions are the enemy of that process.

  • CFOUTPUT GROUP ing max results ??

    Hello All,
    Just a question, is there a max number of rows etc that cold
    fusion can handle in outputing by group from a large query?
    I have a query that returns 45631 rows in about 5 seconds it
    has some joined tables and grouping in the query.
    I output the query using two nested groups
    CFOUTPUT query="get_data" group="suppname"
    Group title
    CFOUTPUT group="mpart"
    relevant part
    CFOUTPUT
    all lines from query for that part
    /CFOUTPUT
    /CFOUTPUT
    /CFOUTPUT
    My page only seems to output 9999 results when I use GROUP in
    the output, but if I just output the query with no GROUPING then I
    get all 45631 results.
    Does CF have a limit on the results when GROUPING in a
    CFOUTPUT?
    Kind Regards Guy

    How are you counting your outputted results?

  • 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

  • Need help in optimizing the query with joins and group by clause

    I am having problem in executing the query below.. it is taking lot of time. To simplify, I have added the two tables FILE_STATUS = stores the file load details and COMM table that is actual business commission table showing records successfully processed and which records were transmitted to other system. Records with status = T is trasnmitted to other system and traansactions with P is pending.
    CREATE TABLE FILE_STATUS
    (FILE_ID VARCHAR2(14),
    FILE_NAME VARCHAR2(20),
    CARR_CD VARCHAR2(5),
    TOT_REC NUMBER,
    TOT_SUCC NUMBER);
    CREATE TABLE COMM
    (SRC_FILE_ID VARCHAR2(14),
    REC_ID NUMBER,
    STATUS CHAR(1));
    INSERT INTO FILE_STATUS VALUES ('12345678', 'CM_LIBM.TXT', 'LIBM', 5, 4);
    INSERT INTO FILE_STATUS VALUES ('12345679', 'CM_HIPNT.TXT', 'HIPNT', 4, 0);
    INSERT INTO COMM VALUES ('12345678', 1, 'T');
    INSERT INTO COMM VALUES ('12345678', 3, 'T');
    INSERT INTO COMM VALUES ('12345678', 4, 'P');
    INSERT INTO COMM VALUES ('12345678', 5, 'P');
    COMMIT;Here is the query that I wrote to give me the details of the file that has been loaded into the system. It reads the file status and commission table to show file name, total records loaded, total records successfully loaded to the commission table and number of records that has been finally transmitted (status=T) to other systems.
    SELECT
        FS.CARR_CD
        ,FS.FILE_NAME
        ,FS.FILE_ID
        ,FS.TOT_REC
        ,FS.TOT_SUCC
        ,NVL(C.TOT_TRANS, 0) TOT_TRANS
    FROM FILE_STATUS FS
    LEFT JOIN
        SELECT SRC_FILE_ID, COUNT(*) TOT_TRANS
        FROM COMM
        WHERE STATUS = 'T'
        GROUP BY SRC_FILE_ID
    ) C ON C.SRC_FILE_ID = FS.FILE_ID
    WHERE FILE_ID = '12345678';In production this query has more joins and is taking lot of time to process.. the main culprit for me is the join on COMM table to get the count of number of transactions transmitted. Please can you give me tips to optimize this query to get results faster? Do I need to remove group and use partition or something else. Please help!

    I get 2 rows if I use my query with your new criteria. Did you commit the record if you are using a second connection to query? Did you remove the criteria for file_id?
    select carr_cd, file_name, file_id, tot_rec, tot_succ, tot_trans
      from (select fs.carr_cd,
                   fs.file_name,
                   fs.file_id,
                   fs.tot_rec,
                   fs.tot_succ,
                   count(case
                            when c.status = 'T' then
                             1
                            else
                             null
                          end) over(partition by c.src_file_id) tot_trans,
                   row_number() over(partition by c.src_file_id order by null) rn
              from file_status fs
              left join comm c
                on c.src_file_id = fs.file_id
             where carr_cd = 'LIBM')
    where rn = 1;
    CARR_CD FILE_NAME            FILE_ID           TOT_REC   TOT_SUCC  TOT_TRANS
    LIBM    CM_LIBM.TXT          12345678                5          4          2
    LIBM    CM_LIBM.TXT          12345677               10          0          0Using RANK can potentially produce multiple rows to be returned though your data may prevent this. ROW_NUMBER will always prevent duplicates. The ordering of the analytical function is irrelevant in your query if you use ROW_NUMBER. You can remove the outermost query and inspect the data returned by the inner query;
    select fs.carr_cd,
           fs.file_name,
           fs.file_id,
           fs.tot_rec,
           fs.tot_succ,
           count(case
                    when c.status = 'T' then
                     1
                    else
                     null
                  end) over(partition by c.src_file_id) tot_trans,
           row_number() over(partition by c.src_file_id order by null) rn
    from file_status fs
    left join comm c
    on c.src_file_id = fs.file_id
    where carr_cd = 'LIBM';
    CARR_CD FILE_NAME            FILE_ID           TOT_REC   TOT_SUCC  TOT_TRANS         RN
    LIBM    CM_LIBM.TXT          12345678                5          4          2          1
    LIBM    CM_LIBM.TXT          12345678                5          4          2          2
    LIBM    CM_LIBM.TXT          12345678                5          4          2          3
    LIBM    CM_LIBM.TXT          12345678                5          4          2          4
    LIBM    CM_LIBM.TXT          12345677               10          0          0          1

  • Rewrite the query with out joins and group by

    Hi,
    This was an interview question.
    Table Names: bookshelf_checkout
    bookshelf
    And the join condition between these two tables is title
    We need to rewrite below query without using join condition and group by clause ?
    SELECT b.title,max(bc.returned_date - bc.checkout_date) "Most Days Out"
               FROM bookshelf_checkout bc,bookshelf b
               WHERE bc.title(+)=b.title
               GROUP BY b.title;When I was in college, I read that most of the SELECT statements can be replaced by basic SQL operations (SET OPERATORS). Now I am trying to rewrite the query with SET operators but not able to get the exact result.
    Kindly help me on this.
    Thanks,
    Suri

    Something like this?
      1  WITH books AS (
      2  SELECT 'title 1' title FROM dual UNION ALL
      3  SELECT 'title 2' FROM dual UNION ALL
      4  SELECT 'title 3' FROM dual ),
      5  bookshelf AS (
      6  SELECT 'title 1' title, DATE '2012-05-01' checkout_date, DATE '2012-05-15' returned_date FROM dual UNION ALL
      7  SELECT 'title 1' title, DATE '2012-05-16' checkout_date, DATE '2012-05-20' returned_date FROM dual UNION ALL
      8  SELECT 'title 2' title, DATE '2012-04-01' checkout_date, DATE '2012-05-15' returned_date FROM dual )
      9  SELECT bs.title, MAX(bs.returned_date - bs.checkout_date) OVER (PARTITION BY title) FROM bookshelf bs
    10  UNION
    11  (SELECT b.title, NULL FROM books b
    12  MINUS
    13* SELECT bs.title, NULL FROM bookshelf bs)
    SQL> /
    TITLE   MAX(BS.RETURNED_DATE-BS.CHECKOUT_DATE)OVER(PARTITIONBYTITLE)
    title 1                                                           14
    title 2                                                           44
    title 3Lukasz

  • Set READ UNCOMMITED for a query with a table join (Ver 12.5. and 15.5)

    Hi all,
    this is an example of how to set the READ UNCOMMITED isolation level for a simple query:
    select col_a from mytable at isolation 0
    What about a query with an equi-join?
    Is it:
    select col_a
    from mytable t1, mytable t2
    at isolation 0
    I am sorry for the simple question, I did not find a auitable example in the documentation.
    Best Regards
    Arthur

    Yeah, the docs aren't very good at providing a robust set of examples so you've gotta:
    1 - review the command syntax and then
    2 - try a few examples until you find the format that works
    1 - From the ASE 15.5 Commands reference manual we find the following syntax:
    =========================
    select ::=
              select [all | distinct]
              [top unsigned_integer]
              select_list
              [into_clause]
              [from_clause]
              [where_clause]
              [group_by_clause]
              [having_clause]
              [order_by_clause]
              [compute_clause]
              [read_only_clause]
              [isolation_clause]
              [browse_clause]
              [plan_clause]
    =========================
    This shows us that the isolation clause is attached at the 'end' of the query as a query-level specifier (along with the 'group by', 'order by' and 'having' clauses).
    From this syntax I'd say your proposed query looks correct so ...
    2 - Try running your example to see if you get a syntax error ("When in doubt, try it out!"), eg:
    =========================
    select s.name,c.name
    from sysobjects s, syscolumns c
    where s.id = c.id
    at isolation 0
    go
    name
    sysobjects
    sysobjects
    sysobjects
    ... snip ...
    =========================

Maybe you are looking for

  • TMG2010 - Front-End (DMZ) / Back-End (Internal) - Reverse Publishing. Exchange 2010

    Hey Guys, I need some help conceptually understanding how TMG2010 is used in a front-end back end scenario. For Example: 1) The client would have TMG2010 in the DMZ 2) The client would have TMG2010 in the Internal Network. 3) The client would have mu

  • Query tuning using TKprof

    Hi all, I have one procedure which takes normally more time to finish, cpu is normal at that time still the procedure takes more time. TKPROF has been generated and find the output below. As i am new with tkprof, what are the points need to noted and

  • Copy-Pasting images to change icons of files/folders.

    I have changed a few folders to images I have found online, rather than the default blue folder, however there is always a background (usually black). Is there some site that has lots of icons with no backgrounds (for example most applications have n

  • UWL workitem error - java.lang.IndexOutofBoundsException

    Any alternative solutions/workaround are welcome. 1. e.g. make the UWL refresh faster. 2.Current Delta Cache validity period or iview is disable for UWL. 3.We encountered the below UWL bugs @ EP 7.0 SPS9, Patched JTECHF level 11 and JTECHS level 12.

  • Adobe Reader 9 and printing labels with USPS.

    I have read some of the other posts about Adobe Reader 9 and printing labels with USPS. I am running Snow Leopard on my IMac with Adobe reader 9.1.3 and have had no problems only when using Mozilla Firefox 3.5.3 for the Mac. There is, however, an ext