Rollup of Counts

I am using Oracle 9i and need assistance with writing an SQL query that shows a hierarchy, with total counts for each level. For example:
Table1: Device_Tree (every device and it's parent device)
col1: Node
col2: Node_Parent
Table2: Meters (a list of meters and their parent)
Col1:MeterId
Col2:Device
I would like to see results similar to the following:
Level Hierarchy MeterCount
1     /LCEC 128
2     /LCEC/B0011 41
3     /LCEC/B0011/F10011 31
4     /LCEC/B0011/F10011/T816 2
4     /LCEC/B0011/F10011/F7897 10
5     /LCEC/B0011/F10011/F7897/T62805 7
5     /LCEC/B0011/F10011/F7897/T11115 3
4     /LCEC/B0011/F10011/T17958 5
4     /LCEC/B0011/F10011/T13052 4
3     /LCEC/B0011/F10012 10
So I would like to see a count of the meters under each level. Any assistance would be appreciated. Thanks.

For 9i:
with device_tree as (
                     select 'LCEC' node,null parent_node from dual union all
                     select 'B0011','LCEC' from dual union all
                     select 'F10011','B0011' from dual union all
                     select 'T816','F10011' from dual union all
                     select 'T7897','F10011' from dual union all
                     select 'T11115','F10011' from dual union all
                     select 'F1234','F10011' from dual union all
                     select 'T333','F1234' from dual union all
                     select 'T554','F1234' from dual
      meters as (
                 select 123 meterid,'T816' device from dual union all
                 select 456,'T816' from dual union all
                 select 788,'T7897' from dual union all
                 select 333,'T7897' from dual union all
                 select 444,'T7897' from dual union all
                 select 555,'T333' from dual union all
                 select 12345,'T554' from dual union all
                 select 789,'T554' from dual union all
                 select 5566,'T1234' from dual
      t as (
            select  rownum rn,
                    level lvl,
                    node,
                    sys_connect_by_path(node,'/') hierarchy,
                    (select count(*) from meters where device = node) meter_count
              from  device_tree
              start with parent_node is null
              connect by parent_node = prior node
select  lvl,
        hierarchy,
         select  sum(t1.meter_count)
           from  t t1
           where t1.hierarchy || '/' like '%/' || t.node || '/%'
        ) meter_count
  from  t
  order by rn
       LVL HIERARCHY                                METER_COUNT
         1 /LCEC                                              8
         2 /LCEC/B0011                                        8
         3 /LCEC/B0011/F10011                                 8
         4 /LCEC/B0011/F10011/T816                            2
         4 /LCEC/B0011/F10011/T7897                           3
         4 /LCEC/B0011/F10011/T11115                          0
         4 /LCEC/B0011/F10011/F1234                           3
         5 /LCEC/B0011/F10011/F1234/T333                      1
         5 /LCEC/B0011/F10011/F1234/T554                      2
9 rows selected.
SQL> SY.

Similar Messages

  • Rollup problem

    Hi,
    I am trying to use rollup to count the number of grade in a course, how many A,B,C, but it didn't come out right. Please help. Thanks.
    ID Name Course Grade
    1 Peter 101 A
    2 Tom 101 A
    3 Jenny 101 B
    I want to display
    as
    ID Name Course Grade
    1 Peter 101 A
    2 Tom 101 A
    Subtotal 101 2
    3 Jenny 101 B
    Subtotal 101 1

    SQL> create table mytable
      2  as
      3  select 1 id, 'Peter' name, 101 course, 'A' grade from dual union all
      4  select 2, 'Tom', 101, 'A' from dual union all
      5  select 3, 'Jenny', 101, 'B' from dual
      6  /
    Tabel is aangemaakt.
    SQL> select id
      2       , decode(grouping(id),1,'Subtotal',name) name
      3       , course
      4       , grade
      5       , decode(grouping(id),1,count(*)) total
      6    from mytable
      7   group by grouping sets ((id,name,course,grade),(course,grade))
      8   order by grade
      9       , id
    10  /
            ID NAME         COURSE G      TOTAL
             1 Peter           101 A
             2 Tom             101 A
               Subtotal        101 A          2
             3 Jenny           101 B
               Subtotal        101 B          1
    5 rijen zijn geselecteerd.Regards,
    Rob.

  • ROLLUP AND CUBE OPERATORS IN ORACLE 8I

    제품 : PL/SQL
    작성날짜 : 2000-06-29
    ========================================
    ROLLUP AND CUBE OPERATORS IN ORACLE 8I
    ========================================
    PURPOSE
    ROLLUP 과 CUBE Operator에 대해 설명하고자 한다.
    Explanation
    ROLLUP operator는 SELECT문의 GROUP BY절에 사용된다.
    SELECT절에 ROLLUP 을 사용함으로써 'regular rows'(보통의 select된 data)와
    'super-aggregate rows'(총계)을 구할 수 있다. 기존에는 select ... union select
    를 이용해 구사해야 했었던 것이다. 'super-aggregate rows'는 'sub-total'
    (중간 Total, 즉 소계)을 포함한다.
    CUBE operator는 Cross-tab에 대한 Summary를 추출하는데 사용된다. 모든 가능한
    dimension에 대한 total을 나타낸다. 즉 ROLLUP에 의해 나타내어지는 item total값과
    column total값을 나타낸다.
    NULL값은 모든 값에 대한 super-aggregate 을 나타낸다. GROUPING() function은
    모든 값에 대한 set을 나타내는 null값과 column의 null값과 구별하는데 쓰여진다.
    GROUPING() function은 GROUP BY절에서 반드시 표현되어야 한다. GROUPING()은 모든
    값의 set을 표현합에 있어서 null이면 1을 아니면 0을 return한다.
    ROLLUP과 CUBE는 CREATE MATERIALIZED VIEW에서 사용되어 질수 있다.
    Example
    아래와 같이 테스트에 쓰여질 table과 data을 만든다.
    create table test_roll
    (YEAR NUMBER(4),
    REGION CHAR(7),
    DEPT CHAR(2),
    PROFIT NUMBER );
    insert into test_roll values (1995 ,'West' , 'A1' , 100);
    insert into test_roll values (1995 ,'West' , 'A2' , 100);
    insert into test_roll values (1996 ,'West' , 'A1' , 100);
    insert into test_roll values (1996 ,'West' , 'A2' , 100);
    insert into test_roll values (1995 ,'Central' ,'A1' , 100);
    insert into test_roll values (1995 ,'East' , 'A1' , 100);
    insert into test_roll values (1995 ,'East' , 'A2' , 100);
    SQL> select * from test_roll;
    YEAR REGION DE PROFIT
    1995 West A1 100
    1995 West A2 100
    1996 West A1 100
    1996 West A2 100
    1995 Central A1 100
    1995 East A1 100
    1995 East A2 100
    7 rows selected.
    예제 1: ROLLUP
    SQL> select year, region, sum(profit), count(*)
    from test_roll
    group by rollup(year, region);
    YEAR REGION SUM(PROFIT) COUNT(*)
    1995 Central 100 1
    1995 East 200 2
    1995 West 200 2
    1995 500 5
    1996 West 200 2
    1996 200 2
    700 7
    7 rows selected.
    위의 내용을 tabular로 나타내어 보면 쉽게 알 수 있다.
    Year Central(A1+A2) East(A1+A2) West(A1+A2)
    1995 (100+NULL) (100+100) (100+100) 500
    1996 (NULL+NULL) (NULL+NULL) (100+100) 200
    700
    예제 2: ROLLUP and GROUPING()
    SQL> select year, region, sum(profit),
    grouping(year) "Y", grouping(region) "R"
    from test_roll
    group by rollup (year, region);
    YEAR REGION SUM(PROFIT) Y R
    1995 Central 100 0 0
    1995 East 200 0 0
    1995 West 200 0 0
    1995 500 0 1
    1996 West 200 0 0
    1996 200 0 1
    700 1 1
    7 rows selected.
    참고) null값이 모든 값의 set에 대한 표현으로 나타내어지면 GROUPING function은
    super-aggregate row에 대해 1을 return한다.
    예제 3: CUBE
    SQL> select year, region, sum(profit), count(*)
    from test_roll
    group by cube(year, region);
    YEAR REGION SUM(PROFIT) COUNT(*)
    1995 Central 100 1
    1995 East 200 2
    1995 West 200 2
    1995 500 5
    1996 West 200 2
    1996 200 2
    Central 100 1
    East 200 2
    West 400 4
    700 7
    위의 내용을 tabular로 나타내어 보면 쉽게 알 수 있다.
    Year Central(A1+A2) East(A1+A2) West(A1+A2)
    1995 (100+NULL) (100+100) (100+100) 500
    1996 (NULL+NULL) (NULL+NULL) (100+100) 200
    100 200 400 700
    예제 4: CUBE and GROUPING()
    SQL> select year, region, sum(profit),
    grouping(year) "Y", grouping(region) "R"
    from test_roll
    group by cube (year, region);
    YEAR REGION SUM(PROFIT) Y R
    1995 Central 100 0 0
    1995 East 200 0 0
    1995 West 200 0 0
    1995 500 0 1
    1996 West 200 0 0
    1996 200 0 1
    Central 100 1 0
    East 200 1 0
    West 400 1 0
    700 1 1
    10 rows selected.
    ===============================================
    HOW TO USE ROLLUP AND CUBE OPERATORS IN PL/SQL
    ===============================================
    Release 8.1.5 PL/SQL에서는 CUBE, ROLLUP 이 지원되지 않는다. 8i에서는 DBMS_SQL
    package을 이용하여 dynamic SQL로 구현하는 방법이 workaround로 제시된다.
    Ordacle8i에서는 PL/SQL block에 SQL절을 직접적으로 위치시키는 Native Dynamic SQL
    (참고 bul#11721)을 지원한다.
    Native Dynamic SQL을 사용하기 위해서는 COMPATIBLE 이 8.1.0 또는 그 보다 높아야
    한다.
    SVRMGR> show parameter compatible
    NAME TYPE VALUE
    compatible string 8.1.0
    예제 1-1: ROLLUP -> 위의 예제 1과 비교한다.
    SQL> create or replace procedure test_rollup as
    my_year test_roll.year%type;
    my_region test_roll.region%type;
    my_sum int;
    my_count int;
    begin
    select year, region, sum(profit), count(*)
    into my_year, my_region, my_sum, my_count
    from test_roll
    group by rollup(year, region);
    end;
    Warning: Procedure created with compilation errors.
    SQL> show errors
    Errors for PROCEDURE TEST_ROLLUP:
    LINE/COL ERROR
    10/8 PL/SQL: SQL Statement ignored
    13/18 PLS-00201: identifier 'ROLLUP' must be declared
    SQL> create or replace procedure test_rollup as
    type curTyp is ref cursor;
    sql_stmt varchar2(200);
    tab_cv curTyp;
    my_year test_roll.year%type;
    my_region test_roll.region%type;
    my_sum int;
    my_count int;
    begin
    sql_stmt := 'select year, region, sum(profit), count(*) ' ||
    'from test_roll ' ||
    'group by rollup(year, region)';
    open tab_cv for sql_stmt;
    loop
    fetch tab_cv into my_year, my_region, my_sum, my_count;
    exit when tab_cv%NOTFOUND;
    dbms_output.put_line (my_year || ' '||
    nvl(my_region,' ') ||
    ' ' || my_sum || ' ' || my_count);
    end loop;
    close tab_cv;
    end;
    SQL> set serveroutput on
    SQL> exec test_rollup
    1995 Central 100 1
    1995 East 200 2
    1995 West 200 2
    1995 500 5
    1996 West 200 2
    1996 200 2
    700 7
    PL/SQL procedure successfully completed.
    예제 2-1: ROLLUP and GROUPING() -> 위의 예제 2와 비교한다.
    SQL> create or replace procedure test_rollupg as
    my_year test_roll.year%type;
    my_region test_roll.region%type;
    my_sum int;
    my_count int;
    my_g_region int;
    my_g_year int;
    begin
    select year, region, sum(profit),
    grouping(year), grouping(region)
    into my_year, my_region, my_sum, my_count,
    my_g_year, my_g_region
    from test_roll
    group by rollup(year, region);
    end;
    Warning: Procedure created with compilation errors.
    SQL> show error
    Errors for PROCEDURE TEST_ROLLUPG:
    LINE/COL ERROR
    12/4 PL/SQL: SQL Statement ignored
    17/13 PLS-00201: identifier 'ROLLUP' must be declared
    SQL> create or replace procedure test_rollupg as
    type curTyp is ref cursor;
    sql_stmt varchar2(200);
    tab_cv curTyp;
    my_year test_roll.year%type;
    my_region test_roll.region%type;
    my_sum int;
    my_count int;
    my_g_region int;
    my_g_year int;
    begin
    sql_stmt := 'select year, region, sum(profit), count(*), ' ||
    'grouping(year), grouping(region) ' ||
    'from test_roll ' ||
    'group by rollup(year, region)';
    open tab_cv for sql_stmt;
    loop
    fetch tab_cv into my_year, my_region, my_sum, my_count,
    my_g_year, my_g_region;
    exit when tab_cv%NOTFOUND;
    dbms_output.put_line (my_year || ' '||my_region ||
    ' ' || my_sum || ' ' || my_count ||
    ' ' || my_g_year || ' ' || my_g_region);
    end loop;
    close tab_cv;
    end;
    Procedure created.
    SQL> set serveroutput on
    SQL> exec test_rollupg
    1995 Central 100 1 0 0
    1995 East 200 2 0 0
    1995 West 200 2 0 0
    1995 500 5 0 1
    1996 West 200 2 0 0
    1996 200 2 0 1
    700 7 1 1
    PL/SQL procedure successfully completed.
    예제 3-1: CUBE -> 위의 예제 3과 비교한다.
    SQL> create or replace procedure test_cube as
    my_year test_roll.year%type;
    my_region test_roll.region%type;
    my_sum int;
    my_count int;
    begin
    select year, region, sum(profit), count(*)
    into my_year, my_region, my_sum, my_count
    from test_roll
    group by cube(year, region);
    end;
    Warning: Procedure created with compilation errors.
    SQL> show error
    Errors for PROCEDURE TEST_CUBE:
    LINE/COL ERROR
    10/4 PL/SQL: SQL Statement ignored
    13/13 PLS-00201: identifier 'CUBE' must be declared
    SQL> create or replace procedure test_cube as
    type curTyp is ref cursor;
    sql_stmt varchar2(200);
    tab_cv curTyp;
    my_year test_roll.year%type;
    my_region test_roll.region%type;
    my_sum int;
    my_count int;
    begin
    sql_stmt := 'select year, region, sum(profit), count(*) ' ||
    'from test_roll ' ||
    'group by cube(year, region)';
    open tab_cv for sql_stmt;
    loop
    fetch tab_cv into my_year, my_region, my_sum, my_count;
    exit when tab_cv%NOTFOUND;
    dbms_output.put_line (my_year || ' '||
    nvl(my_region,' ') ||
    ' ' || my_sum || ' ' || my_count);
    end loop;
    close tab_cv;
    end;
    Procedure created.
    SQL> set serveroutput on
    SQL> exec test_cube
    1995 Central 100 1
    1995 East 200 2
    1995 West 200 2
    1995 500 5
    1996 West 200 2
    1996 200 2
    Central 100 1
    East 200 2
    West 400 4
    700 7
    PL/SQL procedure successfully completed.
    예제 4-1: CUBE and GROUPING() -> 위의 예제 4와 비교한다.
    SQL> create or replace procedure test_cubeg as
    my_year test_roll.year%type;
    my_region test_roll.region%type;
    my_sum int;
    my_count int;
    my_g_region int;
    my_g_year int;
    begin
    select year, region, sum(profit),
    grouping(year), grouping(region)
    into my_year, my_region, my_sum, my_count,
    my_g_year, my_g_region
    from test_roll
    group by cube(year, region);
    end;
    Warning: Procedure created with compilation errors.
    SQL> show error
    Errors for PROCEDURE TEST_CUBEG:
    LINE/COL ERROR
    12/4 PL/SQL: SQL Statement ignored
    17/13 PLS-00201: identifier 'CUBE' must be declared
    SQL> create or replace procedure test_cubeg as
    type curTyp is ref cursor;
    sql_stmt varchar2(200);
    tab_cv curTyp;
    my_year test_roll.year%type;
    my_region test_roll.region%type;
    my_sum int;
    my_count int;
    my_g_region int;
    my_g_year int;
    begin
    sql_stmt := 'select year, region, sum(profit), count(*), ' ||
    'grouping(year), grouping(region) ' ||
    'from test_roll ' ||
    'group by cube(year, region)';
    open tab_cv for sql_stmt;
    loop
    fetch tab_cv into my_year, my_region, my_sum, my_count,
    my_g_year, my_g_region;
    exit when tab_cv%NOTFOUND;
    dbms_output.put_line (my_year || ' '||my_region ||
    ' ' || my_sum || ' ' || my_count ||
    ' ' || my_g_year || ' ' || my_g_region);
    end loop;
    close tab_cv;
    end;
    Procedure created.
    SQL> set serveroutput on
    SQL> exec test_cubeg
    1995 Central 100 1 0 0
    1995 East 200 2 0 0
    1995 West 200 2 0 0
    1995 500 5 0 1
    1996 West 200 2 0 0
    1996 200 2 0 1
    Central 100 1 1 0
    East 200 2 1 0
    West 400 4 1 0
    700 7 1 1
    PL/SQL procedure successfully completed.
    Reference Ducumment
    Note:67988.1

    Hello,
    As previously posted you should use export and import utilities.
    To execute exp or imp statements you have just to open a command line interface, for instance,
    a DOS box on Windows.
    So you don't have to use SQL*Plus or TOAD.
    About export/import you may care on the mode, to export a single or a list of Tables the Table mode
    is enough.
    Please, find here an example to begin:
    http://wiki.oracle.com/page/Oracle+export+and+import+
    Hope this help.
    Best regards,
    Jean-Valentin

  • Multiple counts in a single statement

    Hi,
    I am trying to get a total count of the number of employees in departments as well as the total numbers in specific salary ranges and display it all in one set of results.
    Here is the code I have so far:
    select distinct e.department_id,
    ct1.subct+ct2.subct+ct3.subct+ct4.subct total_num,
    ct1.subct,
    ct2.subct,
    ct3.subct,
    ct4.subct
    from employee e,
    (select department_id, count(department_id) subct from employee where salary < 1500 group by department_id) ct1,
    (select department_id, count(department_id) subct from employee where salary >= 1500 and salary <=2499 group by department_id) ct2,
    (select department_id, count(department_id) subct from employee where salary >= 2500 and salary <=3499 group by department_id) ct3,
    (select department_id, count(department_id) subct from employee where salary >= 3500 group by department_id) ct4
    where e.department_id=ct1.department_id
    and e.department_id=ct2.department_id
    and e.department_id=ct3.department_id
    and e.department_id=ct4.department_id
    The problem with this code is that it only displays departments which have at least one employee in each of the salary ranges in the results. This is because of the join statements, I know, but is there any way around it?
    I know there are some alternative approaches altogether to this problem, using the likes of ROLLUP and so on, but I would like to do it using essentially a similar method to my code above, if that is in fact possible.
    Any assistance would be appreciated!

    Can yu use ROLLUP..?
    SQL> select d.deptno,case when sal < 1500 then 'Low'
      2                       when sal between 1500 and 3000 then 'Medium'
      3                       when sal is null then 'No emp'
      4                       else 'High'
      5                   end sal_range,count(empno)
      6  from dept d,emp e
      7  where d.deptno = e.deptno(+)
      8  group by rollup(d.deptno,case when sal < 1500 then 'Low'
      9                       when sal between 1500 and 3000 then 'Medium'
    10                       when sal is null then 'No emp'
    11                       else 'High'
    12                   end);
        DEPTNO SAL_RA COUNT(EMPNO)
            10 Low               1
            10 High              1
            10 Medium            2
            10                   4
            20 Low               2
            20 Medium            3
            20                   5
            30 Low               3
            30 Medium            3
            30                   6
            40 No emp            0
        DEPTNO SAL_RA COUNT(EMPNO)
            40                   0
            45 No emp            0
            45                   0
                                15
    15 rows selected.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Summary Report with cube and rollup

    hi guys,
    i m working a oracle 9i, i need a summary report after query execution. im using rollup and cube but it is giving me unwanted rows as well. for ex below
    Select
    IRH_REFNO,IRH_TDATE,IRH_BDAMT,IRH_RLOCN, IRH_ILOCN,IRH_EXPECTED_DATE,IRH_ADD_USERID ,
    count(*),
    sum(irh_bdamt)
    from IRH
    where IRH_INTYPE='R'
    and IRH_RLOCN='EDP'
    group by cube(IRH_REFNO,IRH_TDATE,IRH_BDAMT,IRH_RLOCN, IRH_ILOCN,IRH_EXPECTED_DATE,IRH_ADD_USERID )
    Order by IRH_TDATE asc
    i want only two columns to be computed at the end of query. i.e sum of BDAMT and count of rows. i dont want any groupings. any alternate for this.
    thanks n regards
    fkhan

    Please post a create table statement, a few insert statements and the expected output, so we can help you.
    Regards,
    Rob.

  • Count Of a Column

    Hello All,
    I have oracle 10 g r2 on windows.
    i have one table called T. I as selecting two columns that is t_col1 and t_col2. query is :
    select t_col1,count(t_col2) froom T group by t_col1;
    OutPut of the table is :
    t_col1 t_col2
    james 4
    lee 6
    hopes 1
    jack 4.
    now i need sum of the t_col2 as :
    t_col1 t_col2
    james 4
    lee 6
    hopes 1
    jack 4
    Total 15
    for the above result i have created block and declare one variable and insert the sum amount in the variable like :
    declare
    v varchar2(20);
    begin
    select sum(distinct t_col2) into v from T;
    end;
    Problem is the Above Query showing result for every row ie.
    t_col1 t_col2
    james 4 15
    lee 6 15
    hopes 1 15
    jack 4 15
    but i dont want like above. i want it should come below the t_col2 column as TOtal 15.
    is it possible to have sum at the end of the column.
    thnxxx.

    [url http://download.oracle.com/docs/cd/B19306_01/server.102/b14223/aggreg.htm#i1007413]rollup is designed to exactly this:
    SQL> ed
    Wrote file afiedt.buf
      1  with sample_data as
      2  (
      3  select 'james' t_col1, 1 t_col2 from dual union all
      4  select 'james' t_col1, 1 t_col2 from dual union all
      5  select 'james' t_col1, 1 t_col2 from dual union all
      6  select 'james' t_col1, 1 t_col2 from dual union all
      7  select 'lee' t_col1, 1 t_col2 from dual union all
      8  select 'lee' t_col1, 1 t_col2 from dual union all
      9  select 'lee' t_col1, 1 t_col2 from dual union all
    10  select 'lee' t_col1, 1 t_col2 from dual union all
    11  select 'lee' t_col1, 1 t_col2 from dual union all
    12  select 'lee' t_col1, 1 t_col2 from dual union all
    13  select 'hopes' t_col1, 1 t_col2 from dual union all
    14  select 'jack' t_col1, 1 t_col2 from dual union all
    15  select 'jack' t_col1, 1 t_col2 from dual union all
    16  select 'jack' t_col1, 1 t_col2 from dual union all
    17  select 'jack' t_col1, 1 t_col2 from dual
    18  )
    19* select t_col1,count(t_col2) from sample_data group by rollup (t_col1)
    SQL> /
    T_COL COUNT(T_COL2)
    hopes             1
    jack              4
    james             4
    lee               6
                     15and if you need "Total" in last row's t_col1 column, then :
    SQL> ed
    Wrote file afiedt.buf
      1  with sample_data as
      2  (
      3  select 'james' t_col1, 1 t_col2 from dual union all
      4  select 'james' t_col1, 1 t_col2 from dual union all
      5  select 'james' t_col1, 1 t_col2 from dual union all
      6  select 'james' t_col1, 1 t_col2 from dual union all
      7  select 'lee' t_col1, 1 t_col2 from dual union all
      8  select 'lee' t_col1, 1 t_col2 from dual union all
      9  select 'lee' t_col1, 1 t_col2 from dual union all
    10  select 'lee' t_col1, 1 t_col2 from dual union all
    11  select 'lee' t_col1, 1 t_col2 from dual union all
    12  select 'lee' t_col1, 1 t_col2 from dual union all
    13  select 'hopes' t_col1, 1 t_col2 from dual union all
    14  select 'jack' t_col1, 1 t_col2 from dual union all
    15  select 'jack' t_col1, 1 t_col2 from dual union all
    16  select 'jack' t_col1, 1 t_col2 from dual union all
    17  select 'jack' t_col1, 1 t_col2 from dual
    18  )
    19  select t_col1,count(t_col2) from sample_data group by t_col1
    20  union all
    21* select 'Total', sum(count(t_col2)) from sample_data group by t_col1
    SQL> /
    T_COL COUNT(T_COL2)
    james             4
    hopes             1
    jack              4
    lee               6
    Total            15

  • Af:table rollup row background color change

    Hi All
    i got below sql view object
    SELECT DEPARTMENT_ID,JOB_ID,COUNT(*) FROM EMPLOYEES GROUP BY DEPARTMENT_ID,ROLLUP(JOB_ID)
    By using this i will get each department_id, jobid's and total employes count in each department
    and i will get rollup for each departments ie i will get total employees.
    So i want to change the back ground of the total row.
    DepartmentId     JobId     Count1
    SA_REP      1
    1
    10     AD_ASST     1
    10          1 < this row in RED back ground in JSF page
    20     MK_MAN     1
    20     MK_REP     1
    20          2 < this row in RED back ground in JSF page
    30     PU_MAN     1
    30     PU_CLERK     5
    30          6< this row in RED back ground in JSF page
    40     HR_REP     1
    40          1< this row in RED back ground in JSF page
    50     ST_MAN     5
    50     SH_CLERK     20
    50     ST_CLERK     20
    50          45< this row in RED back ground in JSF page
    any kind of help is welcome...
    thanks
    Ravi

    Ravi,
    I think I answered the same question on the JDeveloper forum. The way it goes is that you use the inlineStyle property on the outputText component(s) of the total row. If you hard code the color then all backgrounds are identical, if you use EL to reference a managed bean method to return the color then you can have conditional color rendering
    Frank

  • Using Subquery in Select with Rollup (or some way to get grand total)

    I am trying to do the following (simplified the query):
    SELECT s.SYSTEM,
    (SELECT COUNT(cp1.cid) FROM cfrs cp1, systems sp1, subsystems ssp1 where cp1.priority = 'P1' AND cp1.subsystem = ssp1.subsystem AND sp1.system = ssp1.system AND sp1.system = s.system) AS P1
    FROM cfrs c, systems s, subsystems ss
    WHERE c.subsystem = ss.subsystem
    AND s.system = ss.system
    GROUP BY ROLLUP(s.SYSTEM)
    But the result of the Rollup on the P1 column is always 0 (it is not summing it up)
    How can I accomplish a Rollup (or get a grand total) of the subquery result?
    Thanks,

    How about this then:
    SQL &gt; WITH SYSTEMS AS
      2  (
      3     SELECT 'A' AS SYSTEM FROM DUAL UNION ALL
      4     SELECT 'B' AS SYSTEM FROM DUAL UNION ALL
      5     SELECT 'C' AS SYSTEM FROM DUAL
      6  ),
      7  SUBSYSTEMS AS
      8  (
      9     SELECT 'A' AS SYSTEM, '1' AS SUBSYSTEM FROM DUAL UNION ALL
    10     SELECT 'A' AS SYSTEM, '2' AS SUBSYSTEM FROM DUAL UNION ALL
    11     SELECT 'A' AS SYSTEM, '3' AS SUBSYSTEM FROM DUAL UNION ALL
    12     SELECT 'B' AS SYSTEM, '4' AS SUBSYSTEM FROM DUAL UNION ALL
    13     SELECT 'B' AS SYSTEM, '5' AS SUBSYSTEM FROM DUAL UNION ALL
    14     SELECT 'B' AS SYSTEM, '6' AS SUBSYSTEM FROM DUAL UNION ALL
    15     SELECT 'C' AS SYSTEM, '7' AS SUBSYSTEM FROM DUAL UNION ALL
    16     SELECT 'C' AS SYSTEM, '8' AS SUBSYSTEM FROM DUAL UNION ALL
    17     SELECT 'C' AS SYSTEM, '9' AS SUBSYSTEM FROM DUAL
    18  ),
    19  CFRS AS
    20  (
    21     SELECT 10 AS CID, '1' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
    22     SELECT 11 AS CID, '1' AS SUBSYSTEM, 'LOW' AS PRIORITY FROM DUAL UNION ALL
    23     SELECT 12 AS CID, '1' AS SUBSYSTEM, 'MED' AS PRIORITY FROM DUAL UNION ALL
    24     SELECT 13 AS CID, '2' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
    25     SELECT 14 AS CID, '2' AS SUBSYSTEM, 'MED' AS PRIORITY FROM DUAL UNION ALL
    26     SELECT 15 AS CID, '2' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
    27     SELECT 16 AS CID, '3' AS SUBSYSTEM, 'LOW' AS PRIORITY FROM DUAL UNION ALL
    28     SELECT 17 AS CID, '3' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
    29     SELECT 18 AS CID, '4' AS SUBSYSTEM, 'MED' AS PRIORITY FROM DUAL UNION ALL
    30     SELECT 19 AS CID, '5' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
    31     SELECT 20 AS CID, '5' AS SUBSYSTEM, 'LOW' AS PRIORITY FROM DUAL UNION ALL
    32     SELECT 21 AS CID, '6' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
    33     SELECT 31 AS CID, '7' AS SUBSYSTEM, 'LOW' AS PRIORITY FROM DUAL UNION ALL
    34     SELECT 41 AS CID, '8' AS SUBSYSTEM, 'HIGH' AS PRIORITY FROM DUAL UNION ALL
    35     SELECT 51 AS CID, '9' AS SUBSYSTEM, 'MED' AS PRIORITY FROM DUAL UNION ALL
    36     SELECT 61 AS CID, '9' AS SUBSYSTEM, 'MED' AS PRIORITY FROM DUAL
    37  )
    38  SELECT DECODE(SYSTEM,'ALL_SYSTEMS','Grand Total',SYSTEM) AS SYSTEM
    39  ,  MAX(DECODE(PRIORITY,'LOW',CNT,0))       AS LOW
    40  ,  MAX(DECODE(PRIORITY,'MED',CNT,0))       AS MED
    41  ,  MAX(DECODE(PRIORITY,'HIGH',CNT,0))      AS HIGH
    42  ,  MAX(DECODE(PRIORITY,'ALL_PRIOR',CNT,0)) AS SYSTEM_TOTAL
    43  FROM
    44  (
    45     SELECT DECODE(GROUPING(SYSTEMS.SYSTEM),1,'ALL_SYSTEMS',SYSTEMS.SYSTEM) AS SYSTEM
    46     , DECODE(GROUPING(CFRS.PRIORITY),1,'ALL_PRIOR',CFRS.PRIORITY) AS PRIORITY
    47     , COUNT(*) AS CNT
    48     FROM SYSTEMS
    49     JOIN SUBSYSTEMS ON SYSTEMS.SYSTEM = SUBSYSTEMS.SYSTEM
    50     JOIN CFRS ON SUBSYSTEMS.SUBSYSTEM = CFRS.SUBSYSTEM
    51     GROUP BY CUBE(SYSTEMS.SYSTEM,CFRS.PRIORITY)
    52     ORDER BY SYSTEMS.SYSTEM, CFRS.PRIORITY
    53  )
    54  GROUP BY DECODE(SYSTEM,'ALL_SYSTEMS','Grand Total',SYSTEM)
    55  ORDER BY DECODE(SYSTEM,'ALL_SYSTEMS','Grand Total',SYSTEM)
    56  /
    SYSTEM             LOW        MED       HIGH SYSTEM_TOTAL
    A                    2          2          4            8
    B                    1          1          2            4
    C                    1          2          1            4
    Grand Total          4          5          7           16Oh, BTW, use "code" wrapped in "{}" on either side to post nicely formatted code.

  • Rollup and Display Non Existing Rows

    This query produces the results except that I would like to have a final total and overal average (excluding the "No GPA"). Shown as XXX in the output sample. Can this be done in one query or do I need to union for the summary?
    Additionally, if possible, how can I write some statements so that rows that may not exist can be displayed on the output? Example: there are no GPAs in the range of 0.1 thru 0.9. My query results do not show the first two rows, however, the users would like to see these rows displayed regardless of the existence or not.
    GPARange            Count  Avg
    0.1 - 0.6  (F )            0      0.00
    0.7 - 0.9  (D-)            0      0.00
    1.0 - 1.2  (D )            1     
    1.3 - 1.6  (D+)            1    
    1.7 - 1.9  (C-)            4     
    2.0 - 2.2  (C )           20   
    2.3 - 2.6  (C+)           81
    2.7 - 2.9  (B-)          189
    3.0 - 3.2  (B )          387
    3.3 - 3.6  (B+)          452
    3.7 - 4.9  (A )          316
    NO GPA                   271
    TOTAL                   XXXX    X.XX
    SELECT academic_period,
           stu_population,
           College, 
            CASE
                WHEN ((MBA_GPA)) <= 0.6 then '0.1 - 0.6 (F )'
                WHEN ((MBA_GPA)) <= 0.9 then '0.7 - 0.9 (D-)'
                WHEN ((MBA_GPA)) <= 1.2 then '1.0 - 1.2 (D )'
                WHEN ((MBA_GPA)) <= 1.6 then '1.3 - 1.6 (D+)'
                WHEN ((MBA_GPA)) <= 1.9 then '1.7 - 1.9 (C-)'
                WHEN ((MBA_GPA)) <= 2.2 then '2.0 - 2.2 (C )'
                WHEN ((MBA_GPA)) <= 2.6 then '2.3 - 2.6 (C+)'
                WHEN ((MBA_GPA)) <= 2.9 then '2.7 - 2.9 (B-)'
                WHEN ((MBA_GPA)) <= 3.2 then '3.0 - 3.2 (B )'
                WHEN ((MBA_GPA)) <= 3.6 then '3.3 - 3.6 (B+)'
                WHEN ((MBA_GPA)) >  3.6 then '3.7 - 4.9 (A-)'
                WHEN ((MBA_GPA)) is null then 'No GPA'
            ELSE
                'What Happened'
           END as GPARange,
           count(id), round(avg(mba_gpa),2) AVG_GPA
    FROM gpa_stat
    where stu_population='F'
    and academic_period ='200940'
    GROUP BY  stu_population, 
              academic_period,
            CASE
                WHEN ((MBA_GPA)) <= 0.6 then '0.1 - 0.6 (F )'
                WHEN ((MBA_GPA)) <= 0.9 then '0.7 - 0.9 (D-)'
                WHEN ((MBA_GPA)) <= 1.2 then '1.0 - 1.2 (D )'
                WHEN ((MBA_GPA)) <= 1.6 then '1.3 - 1.6 (D+)'
                WHEN ((MBA_GPA)) <= 1.9 then '1.7 - 1.9 (C-)'
                WHEN ((MBA_GPA)) <= 2.2 then '2.0 - 2.2 (C )'
                WHEN ((MBA_GPA)) <= 2.6 then '2.3 - 2.6 (C+)'
                WHEN ((MBA_GPA)) <= 2.9 then '2.7 - 2.9 (B-)'
                WHEN ((MBA_GPA)) <= 3.2 then '3.0 - 3.2 (B )'
                WHEN ((MBA_GPA)) <= 3.6 then '3.3 - 3.6 (B+)'
                WHEN ((MBA_GPA)) >  3.6 then '3.7 - 4.9 (A-)'
                WHEN ((MBA_GPA)) is null then 'No GPA'
            ELSE
                'What Happened'
           END,
              rollup(college)
    order by 1, 2, 3, 4;

    Hi,
    user1069723 wrote:
    This query produces the results except that I would like to have a final total and overal average (excluding the "No GPA"). Shown as XXX in the output sample. Can this be done in one query or do I need to union for the summary?Yes, you can do that in one query, using ROLLUP. You don't need a UNION.
    Additionally, if possible, how can I write some statements so that rows that may not exist can be displayed on the output? Example: there are no GPAs in the range of 0.1 thru 0.9. My query results do not show the first two rows, however, the users would like to see these rows displayed regardless of the existence or not.That's exactly what outer joins do: they display data from one table with matching data from a second table, if any matching data is found. The data from the first table is displayed whether or not there is a match. If there is no match, all columns from the second table are NULL. If you want to treat those NULLs as 0's, use NVL, as I did when displayijng the average, below.
    In this problem, what is the first table, the table that has one row per grade range, and will be displayed even if no row in the result set falls into that grade range?
    You ought to have such a table. The information about where the ranges begin and end, what letter grades are associated with each, and so on, is data. Data belongs in tables, not hard-coded into queries. If, next year, they decide to change where the boundary between D+ and C- falls, or to add an A group, an authorized user can make the change to one table. No matter how many queries use grade ranges, no programmer will have to change any query.
    Here's how you might create such a table:
    CREATE TABLE     GPA_Range
    AS
    SELECT  0.0 AS low_gpa, 0.7 AS high_gpa, '0.1 - 0.6 (F)'  AS display_txt, 'F'  AS letter_txt     FROM dual     UNION ALL
    SELECT  0.7 AS low_gpa, 1.0 AS high_gpa, '0.7 - 0.9 (D-)' AS display_txt, 'D-' AS letter_txt     FROM dual     UNION ALL
    SELECT  1.0 AS low_gpa, 1.3 AS high_gpa, '1.0 - 1.2 (D)'  AS display_txt, 'D'  AS letter_txt     FROM dual     UNION ALL
    SELECT  1.3 AS low_gpa, 1.7 AS high_gpa, '1.3 - 1.6 (D+)' AS display_txt, 'D+' AS letter_txt     FROM dual     UNION ALL
    SELECT  1.7 AS low_gpa, 2.0 AS high_gpa, '1.7 - 1.9 (C-)' AS display_txt, 'C-' AS letter_txt     FROM dual     UNION ALL
    SELECT  2.0 AS low_gpa, 2.3 AS high_gpa, '2.0 - 2.2 (C)'  AS display_txt, 'C'  AS letter_txt     FROM dual     UNION ALL
    SELECT  2.3 AS low_gpa, 2.7 AS high_gpa, '2.3 - 2.6 (C+)' AS display_txt, 'C+' AS letter_txt     FROM dual     UNION ALL
    SELECT  2.7 AS low_gpa, 3.0 AS high_gpa, '2.7 - 2.9 (B-)' AS display_txt, 'B-' AS letter_txt     FROM dual     UNION ALL
    SELECT  3.0 AS low_gpa, 3.3 AS high_gpa, '3.0 - 3.2 (B)'  AS display_txt, 'B'  AS letter_txt     FROM dual     UNION ALL
    SELECT  3.3 AS low_gpa, 3.7 AS high_gpa, '3.3 - 3.6 (B+)' AS display_txt, 'B+' AS letter_txt     FROM dual     UNION ALL
    SELECT  3.7 AS low_gpa, 999 AS high_gpa, '3.7 - 4.9 (A-)' AS display_txt, 'A-' AS letter_txt     FROM dual     UNION ALL
    SELECT  NULL,            NULL,             'No GPA',                         NULL             FROM dual;As always, document everything, especially non-intuitive or potentially misleading things:
    COMMENT ON COLUMN gpa_range.low_gpa IS 'Lowest numerical grade inside this range.';
    COMMENT ON COLUMN gpa_range.low_gpa IS 'Lowest numerical grade in THE NEXT range.  This value is OUTSIDE of the range';You might prefer to assign impossible low_gpa and high_gpa values (like -2 and -1) to the 'No GPA' row. It's completely arbitrary, non-intutive and potentially confusing, but it would make joining easier.
    If you can't create such a table, you can use a sub-query almost identical to the CREATE TABLE statement above in every query that needs it.
    Here's the main query to do what you requested:
    WITH      interesting_gpa_stat     AS
         SELECT     college
         ,     mba_gpa
         FROM     gpa_stat
         WHERE     stu_population     = 'F'
         AND     academic_period     = '200940'
    ,     all_colleges     AS
         SELECT DISTINCT     college
         FROM             interesting_gpa_stat
    SELECT        c.college
    ,        r.display_txt           AS gparange
    ,        COUNT (s.college)             AS count
    ,        NVL ( AVG (s.mba_gpa)
                , 0
                )               AS avg
    FROM                all_colleges          c
    CROSS JOIN        gpa_range          r
    LEFT OUTER JOIN        interesting_gpa_stat     s     ON (     c.college     =  s.college
                                                AND     r.low_gpa     <= s.mba_gpa
                                              AND     r.high_gpa     >  s.mba_gpa
                                  OR (     c.college     =  s.college
                                     AND     r.low_gpa     IS NULL
                                           AND     s.mba_gpa     IS NULL
    GROUP BY  ROLLUP ( c.college
                      , r.display_txt
    ORDER BY  c.college
    ,            r.display_txtWithout any sample data and the results you want from that data, I had to make several assumptions.
    For example, I assumed you wanted to have several colleges in the same result set. For each college, there will be one row of output for every grade range in the gpa_range table, regardless of whether any matches are found in the gpa_stat table. It's okay if there happens to be only one college if the result set.
    The query includes a total for each college, and a grand total for the entire result set.
    Any filtering (such as stu_population='F') should be done in the interesting_gpa_stat sub-query. You could get by without this sub-query, but any filtering conditions that would be in the WHERE clause would have to be added to outer-join conditions.
    NULL values are ignored in all aggregate funcitons, so nothing special has to be done to keep rows in the 'No GPA' group from distorting the averages. The NVL function that presents 0 as the average when no grades were found in the other ranges always causes this average to apperar as 0 in the 'No GPA' rows, too. If you would prefer NULL in that column, then put the NVL inside a CASE expression.
    Edited by: Frank Kulash on Jun 28, 2009 5:03 AM

  • Count number of rows - question

    Hi,
    I have a quite simple SQL statement, and I need to count the nummer of rows which are grouped.
    select
    to_char(em_date_time,'DD-MON-YYYY') dt,
    session_id,
    location_name,
    sc_name,
    transaction_name,
    em_status_id,
    AVG(em_result_value) "MID_RESULT_VALUE"
    from
    tabel_test
    group by
    to_char(em_date_time,'DD-MON-YYYY'),
    session_id,
    location_name,
    sc_name,
    transaction_name,
    em_status_id
    Does anyone know how to do this?
    Thanks for help,
    Walter

    Something like that ?
    SQL> select deptno,
      2  decode(grouping(deptno),0,avg(sal),1,null) avg,
      3  count(*) from emp group by rollup(deptno)
      4  /
        DEPTNO        AVG   COUNT(*)
            10 1458,33333          3
            20     1107,5          5
            30 830,833333          6
                                  14Rgds.

  • Group by rollup - inconsistent?

    Ran across this when doing some aggregates with real data, where the difference is more pronounced. But this test case shows it:
    with t as (
    select case
             when dbms_random.value(0,2) < 1 then 10
                                             else 20
           end dept,
           case
             when dbms_random.value(0,2) < 1 then 'abc'
                                             else 'def'
           end marker,
           sysdate - 20 + trunc(dbms_random.value(1,4)) new_date,
           sysdate - 20 + trunc(dbms_random.value(4,7)) old_date
    from dual
    connect by level<=20000)
    select dept,
           marker,
           diff,
           COUNT(*) / MAX(cnt) percent
    from (select t.*,
                 old_date - new_date diff,
                 COUNT(*) over (partition by dept, marker) cnt
          from t)
    group by rollup(dept,
                    marker,
                    diff)
    order by dept,
             marker,
             diffSo this generates 20,000 random records, groups them depending on how far apart two dates are, and then displays the % that each difference makes of that grouping. A ROLLUP isn't really necessary - I just put it in because I wanted to verify, quickly, that it was adding up to 100% for each group.
    Now, here's a sample output:
    DEPT                   MARKER DIFF                   PERCENT               
    10                     abc    1                      0.1125854443104141535987133092078809811017
    10                     abc    2                      0.2088862082830719742661841576196220345798
    10                     abc    3                      0.3331322878970647366304784881383192601528
    10                     abc    4                      0.2316043425814234016887816646562123039807
    10                     abc    5                      0.113791716928025733815842380377965420185
    10                     abc                           1                     
    10                     def    1                      0.1072504518979714802169110263105041172926
    10                     def    2                      0.2185177746535448885318337015464952801767
    10                     def    3                      0.3426390841534444667603936533440449889536
    10                     def    4                      0.2217312713396264310102430206868849166499
    10                     def    5                      0.1098614179554127334806185981120706969271
    10                     def                           1                     
    10                                                   1.9989957822855995179754970877686282386
    20                     abc    1                      0.11625148279952550415183867141162514828
    20                     abc    2                      0.2196520363780150257018584420719652036378
    20                     abc    3                      0.3301700276789244760775009885330170027679
    20                     abc    4                      0.2178726769474100434954527481217872676947
    20                     abc    5                      0.1160537761961249505733491498616053776196
    20                     abc                           1                     
    20                     def    1                      0.1060332732010422930446983363399478853478
    20                     def    2                      0.2136700741631589496893164962918420525155
    20                     def    3                      0.3553818400481058328322309079975947083584
    20                     def    4                      0.2156744838645019041892162758067749047905
    20                     def    5                      0.1092403287231910202445379835638404489878
    20                     def                           1                     
    20                                                   1.98635824436536180308422301304863582444
                                                         3.9541320680110715697904310003954132068
    27 rows selectedNow, with enough records I would expect some inaccuracy - this is a computer, and roundoff errors are a fact of life. The part that confuses me is that the first level of aggregation is always the exact "1", indicating a perfect 100% - so there is no roundoff error occuring there. But further rollups start to show some error.
    Any ideas on why some would show roundoff errors while the others do not? With my actual data, it's much more pronounced - only getting ~142% out of what should be 200% for one, 193% out of 200% for the second, and the overall total is only ~211% out of 400%.

    Figured out how to show the "very weird" results - scewed data:
    with t as (
    select case
             when dbms_random.value(0,2) < .7 then 10
                                             else 20
           end dept,
           case
             when dbms_random.value(0,2) < .5 then 'abc'
                                             else 'def'
           end marker,
           sysdate - 20 + trunc(dbms_random.value(1,4)*5) new_date,
           sysdate - 20 + trunc(dbms_random.value(4,7)*5) old_date
    from dual
    connect by level<=20000)
    select dept,
           marker,
           case
             when diff < 3 then '1-2'
             when diff < 8 then '3-7'
             when diff < 17 then '8-16'
                            else '17 or more'
           end diff_range,
           COUNT(*) / MAX(cnt) percent
    from (select t.*,
                 old_date - new_date diff,
                 COUNT(*) over (partition by dept, marker) cnt
          from t)
    group by rollup(dept,
                    marker,
                    case
             when diff < 3 then '1-2'
             when diff < 8 then '3-7'
             when diff < 17 then '8-16'
                            else '17 or more'
           end)
    order by dept,
             marker,
             length(diff_range),
             diff_range
    DEPT                   MARKER DIFF_RANGE PERCENT               
    10                     abc    1-2        0.0121472621939824331900579330966174546814
    10                     abc    3-7        0.1113810502709773874042235096243692767707
    10                     abc    8-16       0.4692580826013829190805456923939450569987
    10                     abc    17 or more 0.4072136049336572603251728648850682115492
    10                     abc               1                     
    10                     def    1-2        0.0155172413793103448275862068965517241379
    10                     def    3-7        0.1143678160919540229885057471264367816092
    10                     def    8-16       0.4614942528735632183908045977011494252874
    10                     def    17 or more 0.4086206896551724137931034482758620689655
    10                     def               1                     
    10                                       1.32517286488506821154924313212483647916
    20                     abc    1-2        0.0124575311438278595696489241223103057758
    20                     abc    3-7        0.109132091012045711932461649335941521672
    20                     abc    8-16       0.4739009574796664264387933697106970040152
    20                     abc    17 or more 0.404509420364460002059096056831051168537
    20                     abc               1                     
    20                     def    1-2        0.010325406758448060075093867334167709637
    20                     def    3-7        0.1129536921151439299123904881101376720901
    20                     def    8-16       0.4693366708385481852315394242803504380476
    20                     def    17 or more 0.4073842302878598247809762202753441802253
    20                     def               1                     
    20                                       1.32904354988160197673221455780912179553
                                             2.05909605683105116853701225162153814475
    23 rows selected

  • Select count distinct problem

    Hi,
    I have a tabe "M" that as messages, each message has a priority and is part of a departement. Now i wanted to create a report that count the number of messeges by departement group by priority and the total.............. something like this:
    Departement Priority1 Priority2 Priority3 Total
    A 1 2 3 6
    B 2 3 3 8
    C 1 1 1 3
    Can someboby help me please
    Than's
    Luis

    Unfortunately prior to 11g Oracle doesn't supply any means of creating cross tab queries, so any solution prior to 11g is going to rely on static views.
    However, if you don't mind having the data go down instead of accross you could use this:
    with m as (select 'high' priority, 'department A' department, 1 id_message from dual
      union all select 'medium', 'department A', 2 from dual
      union all select 'high',   'department B', 3 from dual
      union all select 'medium', 'department C', 4 from dual
      union all select 'high',   'department C', 5 from dual
      union all select 'medium', 'department B', 6 from dual
      union all select 'low',    'department A', 7 from dual
      union all select 'medium', 'department A', 8 from dual
      union all select 'low',    'department A', 9 from dual
      union all select 'high',   'department B', 10 from dual
      union all select 'low',    'department C', 11 from dual
      union all select 'medium', 'department C', 12 from dual
      union all select 'high',   'department B', 13 from dual
      union all select 'medium', 'department B', 14 from dual
      union all select 'medium', 'department B', 15 from dual
      union all select 'low',    'department B', 16 from dual
      union all select 'low',    'department C', 17 from dual
      union all select 'high',   'department C', 18 from dual
    select department, priority, count(id_message) from m
    group by rollup (department, priority)
    order by department, priority
    DEPARTMENT   PRIORITY COUNT(ID_MESSAGE)
    department A high     1
    department A low      2
    department A medium   2
    department A (null)   5
    department B high     3
    department B low      1
    department B medium   3
    department B (null)   7
    department C high     2
    department C low      2
    department C medium   2
    department C (null)   6
    (null)       (null)   18
    13 rows selectedor
    select department, priority, count(id_message) from m
    group by department, rollup (priority)
    order by department, priority
    DEPARTMENT   PRIORITY COUNT(ID_MESSAGE)
    department A high     1
    department A low      2
    department A medium   2
    department A (null)   5
    department B high     3
    department B low      1
    department B medium   3
    department B (null)   7
    department C high     2
    department C low      2
    department C medium   2
    department C (null)   6
    12 rows selected

  • Upgrade of Exchange 2007 SP2 rollup 5 to SP3

    Hi,
    We are trying to Upgrade Our Exchange 2007 server from todays SP2 With rollup 5 to the SP3 package.
    During the installation it stops right after stopping all of the services and asks for the exchangeserver..msi file, if i point it the that file in the SP3 package it still just asks me for the file again.
    Exchange Server is running on a Windows Server 2008 system
    Any ideas of what the solution could be?
    e is what i have from the exchangesetup.txt:
    [02.04.2014 22:56:54] [0] Setup will run the task 'uninstall-msipackage'
    [02.04.2014 22:56:54] [1] Setup launched task 'uninstall-msipackage -logfile 'C:\ExchangeSetupLogs\ExchangeSetup.msilog' -ProductCode '24b2c164-de66-44fe-b468-a46d9d5e6b31' -PropertyValues 'BYPASS_CONFIGURED_CHECK=1 DEFAULTLANGUAGENAME=ENU'' 
    [02.04.2014 22:56:54] [1] Beginning processing.
    [02.04.2014 22:56:54] [1] Property 'PackageName' is 'exchangeserver.msi'.
    [02.04.2014 22:56:54] [1] Removing MSI package with code '24b2c164-de66-44fe-b468-a46d9d5e6b31'.
    [02.04.2014 22:59:50] [1] [ERROR] Unexpected Error
    [02.04.2014 22:59:50] [1] [ERROR] Unable to remove product with code 24b2c164-de66-44fe-b468-a46d9d5e6b31. The installation source for this product is not available. Verify that the source exists and that you can access it. Error code is 1612.
    [02.04.2014 22:59:50] [1] [ERROR] The installation source for this product is not available. Verify that the source exists and that you can access it
    [02.04.2014 22:59:50] [1] Ending processing.
    [02.04.2014 22:59:50] [0] The Exchange Server Setup operation did not complete. For more information, visit
    http://support.microsoft.com and enter the Error ID.
    [02.04.2014 22:59:50] [0] End of Setup
    [02.04.2014 22:59:50] [0] **********************************************
    And here is the relevant from the exchangesetup.msilog file:
    === Verbose logging started: 02.04.2014  22:56:54  Build type: SHIP UNICODE 4.05.6002.00  Calling process: D:\Temp\exchange\Setup\ServerRoles\Common\ExSetup.exe ===
    MSI (c) (48:20) [22:56:54:795]: Resetting cached policy values
    MSI (c) (48:20) [22:56:54:795]: Machine policy value 'Debug' is 0
    MSI (c) (48:20) [22:56:54:795]: ******* RunEngine:
               ******* Product: {24B2C164-DE66-44FE-B468-A46D9D5E6B31}
               ******* Action:
               ******* CommandLine: **********
    MSI (c) (48:20) [22:56:54:796]: Client-side and UI is none or basic: Running entire install on the server.
    MSI (c) (48:20) [22:56:54:796]: Grabbed execution mutex.
    MSI (c) (48:20) [22:56:54:800]: Cloaking enabled.
    MSI (c) (48:20) [22:56:54:800]: Attempting to enable all disabled privileges before calling Install on Server
    MSI (c) (48:20) [22:56:54:809]: Incrementing counter to disable shutdown. Counter after increment: 0
    MSI (s) (34:B8) [22:56:54:815]: Running installation inside multi-package transaction {24B2C164-DE66-44FE-B468-A46D9D5E6B31}
    MSI (s) (34:B8) [22:56:54:815]: Grabbed execution mutex.
    MSI (s) (34:88) [22:56:54:818]: Resetting cached policy values
    MSI (s) (34:88) [22:56:54:818]: Machine policy value 'Debug' is 0
    MSI (s) (34:88) [22:56:54:818]: ******* RunEngine:
               ******* Product: {24B2C164-DE66-44FE-B468-A46D9D5E6B31}
               ******* Action:
               ******* CommandLine: **********
    MSI (s) (34:88) [22:56:54:819]: Machine policy value 'DisableUserInstalls' is 0
    MSI (s) (34:88) [22:56:54:820]: Warning: Local cached package 'C:\Windows\Installer\b335acd4.msi' is missing.
    MSI (s) (34:88) [22:56:54:820]: User policy value 'SearchOrder' is 'nmu'
    MSI (s) (34:88) [22:56:54:820]: User policy value 'DisableMedia' is 0
    MSI (s) (34:88) [22:56:54:820]: Machine policy value 'AllowLockdownMedia' is 0
    MSI (s) (34:88) [22:56:54:820]: SOURCEMGMT: Media enabled only if package is safe.
    MSI (s) (34:88) [22:56:54:820]: SOURCEMGMT: Looking for sourcelist for product {24B2C164-DE66-44FE-B468-A46D9D5E6B31}
    MSI (s) (34:88) [22:56:54:820]: SOURCEMGMT: Adding {24B2C164-DE66-44FE-B468-A46D9D5E6B31}; to potential sourcelist list (pcode;disk;relpath).
    MSI (s) (34:88) [22:56:54:820]: SOURCEMGMT: Now checking product {24B2C164-DE66-44FE-B468-A46D9D5E6B31}
    MSI (s) (34:88) [22:56:54:820]: SOURCEMGMT: Media is enabled for product.
    MSI (s) (34:88) [22:56:54:820]: SOURCEMGMT: Attempting to use LastUsedSource from source list.
    MSI (s) (34:88) [22:56:54:821]: SOURCEMGMT: Trying source C:\Users\administrator.LKG\Desktop\Exchange 2007 sp2\.
    MSI (s) (34:88) [22:56:54:821]: Note: 1: 1402 2: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer 3: 2
    MSI (s) (34:88) [22:56:54:821]: Note: 1: 2203 2: C:\Users\administrator.LKG\Desktop\Exchange 2007 sp2\exchangeserver.msi 3: -2147287037
    MSI (s) (34:88) [22:56:54:821]: SOURCEMGMT: Source is invalid due to missing/inaccessible package.
    MSI (s) (34:88) [22:56:54:821]: Note: 1: 1706 2: -2147483647 3: exchangeserver.msi
    MSI (s) (34:88) [22:56:54:821]: SOURCEMGMT: Processing net source list.
    MSI (s) (34:88) [22:56:54:821]: Note: 1: 1706 2: -2147483647 3: exchangeserver.msi
    MSI (s) (34:88) [22:56:54:821]: SOURCEMGMT: Processing media source list.
    MSI (s) (34:88) [22:56:54:824]: Note: 1: 2203 2:  3: -2147287037
    MSI (s) (34:88) [22:56:54:824]: SOURCEMGMT: Source is invalid due to missing/inaccessible package.
    MSI (s) (34:88) [22:56:54:824]: Note: 1: 1706 2: -2147483647 3: exchangeserver.msi
    MSI (s) (34:88) [22:56:54:824]: SOURCEMGMT: Processing URL source list.
    MSI (s) (34:88) [22:56:54:824]: Note: 1: 1402 2: UNKNOWN\URL 3: 2
    MSI (s) (34:88) [22:56:54:824]: Note: 1: 1706 2: -2147483647 3: exchangeserver.msi
    MSI (s) (34:88) [22:56:54:824]: Note: 1: 1706 2:  3: exchangeserver.msi
    MSI (c) (48:F0) [22:56:54:825]: User policy value 'SearchOrder' is 'nmu'
    MSI (c) (48:F0) [22:56:54:825]: User policy value 'DisableMedia' is 0
    MSI (c) (48:F0) [22:56:54:825]: Machine policy value 'AllowLockdownMedia' is 0
    MSI (c) (48:F0) [22:56:54:825]: SOURCEMGMT: Media enabled only if package is safe.
    MSI (c) (48:F0) [22:56:54:825]: SOURCEMGMT: Prompting user for a valid source.
    MSI (c) (48:F0) [22:56:54:825]: Machine policy value 'DisableBrowse' is 0
    MSI (c) (48:F0) [22:56:54:825]: Machine policy value 'AllowLockdownBrowse' is 0
    MSI (c) (48:F0) [22:56:54:825]: SOURCEMGMT: Browsing is enabled.
    MSI (c) (48:F0) [22:56:54:830]: Font created.  Charset: Req=0, Ret=0, Font: Req=MS Shell Dlg, Ret=MS Shell Dlg
    MSI (c) (48:F0) [22:56:54:830]: Machine policy value 'DisableUserInstalls' is 0
    MSI (c) (48:F0) [22:56:54:831]: SOURCEMGMT: Now checking product {24B2C164-DE66-44FE-B468-A46D9D5E6B31}
    MSI (c) (48:F0) [22:56:54:831]: SOURCEMGMT: Media is enabled for product.
    MSI (c) (48:F0) [22:56:54:831]: SOURCEMGMT: Attempting to use LastUsedSource from source list.
    MSI (c) (48:F0) [22:56:54:831]: Note: 1: 1706 2:  3: exchangeserver.msi
    MSI (c) (48:F0) [22:56:54:831]: SOURCEMGMT: Processing net source list.
    MSI (c) (48:F0) [22:56:54:831]: Note: 1: 1706 2: -2147483647 3: exchangeserver.msi
    MSI (c) (48:F0) [22:56:54:831]: SOURCEMGMT: Processing media source list.
    MSI (c) (48:F0) [22:56:54:831]: SOURCEMGMT: Trying media source ;1.
    MSI (c) (48:F0) [22:56:54:831]: Note: 1: 1706 2:  3: exchangeserver.msi
    MSI (c) (48:F0) [22:56:54:831]: SOURCEMGMT: Processing URL source list.
    MSI (c) (48:F0) [22:56:54:831]: Note: 1: 1402 2: UNKNOWN\URL 3: 2
    MSI (c) (48:F0) [22:56:54:831]: Note: 1: 1706 2: -2147483647 3: exchangeserver.msi
    MSI (c) (48:F0) [22:56:54:831]: Note: 1: 1706 2:  3: exchangeserver.msi
    MSI (c) (48:14) [22:58:49:184]: SOURCEMGMT: Trying source D:\Temp\exchange\exchangeserver.msi.
    MSI (c) (48:14) [22:58:49:184]: Note: 1: 1402 2: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\ExpMSI (s) (34:B8) [22:59:50:653]: I/O on thread 5248 could not be cancelled. Error: 1168
    MSI (s) (34:B8) [22:59:50:653]: I/O on thread 6584 could not be cancelled. Error: 1168
    MSI (s) (34:B8) [22:59:50:653]: I/O on thread 3208 could not be cancelled. Error: 1168
    MSI (s) (34:B8) [22:59:50:653]: I/O on thread 9372 could not be cancelled. Error: 1168
    MSI (s) (34:88) [22:59:50:654]: SOURCEMGMT: Failed to resolve source
    MSI (s) (34:88) [22:59:50:655]: MainEngineThread is returning 1612
    lorer 3: 2
    MSI (c) (48:14) [22:58:49:251]: SOURCEMGMT: Source is invalid due to client source out of sync (product code is the same).
    MSI (c) (48:14) [22:58:49:252]: Note: 1: 1731 2: -2147483645 3: exchangeserver.msi
    MSI (s) (34:B8) [22:59:50:656]: User policy value 'DisableRollback' is 0
    MSI (s) (34:B8) [22:59:50:656]: Machine policy value 'DisableRollback' is 0
    MSI (s) (34:B8) [22:59:50:657]: Incrementing counter to disable shutdown. Counter after increment: 0
    MSI (s) (34:B8) [22:59:50:657]: Note: 1: 1402 2: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Installer\Rollback\Scripts 3: 2
    MSI (s) (34:B8) [22:59:50:658]: Note: 1: 1402 2: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Installer\Rollback\Scripts 3: 2
    MSI (s) (34:B8) [22:59:50:659]: Note: 1: 1402 2: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Installer\InProgress 3: 2
    MSI (s) (34:B8) [22:59:50:659]: Note: 1: 1402 2: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Installer\InProgress 3: 2
    MSI (s) (34:B8) [22:59:50:659]: Decrementing counter to disable shutdown. If counter >= 0, shutdown will be denied.  Counter after decrement: -1
    MSI (s) (34:B8) [22:59:50:660]: Restoring environment variables
    MSI (c) (48:20) [22:59:50:663]: Decrementing counter to disable shutdown. If counter >= 0, shutdown will be denied.  Counter after decrement: -1
    MSI (c) (48:20) [22:59:50:664]: MainEngineThread is returning 1612
    === Verbose logging stopped: 02.04.2014  22:59:50 ===

    Hi
    Its the problem with the installation file  
    Its better you download a fresh installation pack from the tech-net and start the upgrade and it will be successful probably the latest one below SP3 RU9
    http://www.microsoft.com/en-us/download/details.aspx?id=36016
    Remember to mark as helpful if you find my contribution useful or as an answer if it does answer your question.That will encourage me - and others - to take time out to help you
    But that download is only the rollup 9 for the SP3.
    The current system is SP2 With rollup 5, will i not have to Upgrade With the SP3 package first?
    Before moving on to the rollups?

  • Re Count statement n times using the same column

    Hi All,
    I am struggling with a query. I am using a column to count using a wildcard for 'N1%', I want to be able to also count for 'N2%' up to 'N5%' in the query.
    My query is as follows:
    SELECT s.name as name, count (cs.pri_comment_txt) as N1
    FROM systems s, clientstate cs
    WHERE cs.supportpriority is NOT NULL
    AND cs.assignedname is null
    AND s.systemid = cs.systemtype
    AND cs.pri_comment_txt like 'N1%'
    group by rollup (s.name)
    Thanks inadvance
    Regards

    I think you mean something like this (untested)?
    SELECT s.name as name
    , count (case when cs.pri_comment_txt like 'N1%' then 1 else null end) as N1
    , count (case when cs.pri_comment_txt like 'N2%' then 1 else null end) as N2
    , count (case when cs.pri_comment_txt like 'N3%' then 1 else null end) as N3
    , count (case when cs.pri_comment_txt like 'N4%' then 1 else null end) as N4
    , count (case when cs.pri_comment_txt like 'N5%' then 1 else null end) as N5
    from systems s
    , clientstate cs
    where cs.supportpriority is not null
    and cs.assignedname is null
    and s.systemid = cs.systemtype
    and (
       cs.pri_comment_txt like 'N1%'
       or cs.pri_comment_txt like 'N2%'
       or cs.pri_comment_txt like 'N3%'
       or cs.pri_comment_txt like 'N4%'
       or cs.pri_comment_txt like 'N5%'
    group by rollup (s.name);cheers,
    Anthony

  • Best way to loop through multiple rows to search for a count result

    I have a script where I'm trying to find a Rollup row without any children. I have created a SQL statement to do this, but I can only input one point at a time. If the count is greater than 0 then i'm good to go. If the count is 0 then I want to know about it.
    select count (*) from (Select * From ESSBASE_FCS.Ham
    where hier_pt like (Select substr(hier_pt, 1,8)||'%' as hcy_pt
    From ESSBASE_FCS.Ham
    Where Hier_Pt = '412375....')
    And Cctr_Or_Rollup 'Rollup')
    What is the best way to develop a SQL script that searches through all my Hier_pt's and return everything with a count of 0, instead of manually inputing each hier_pt at a time?
    Thanks for any help.

    Hi,
    Please read SQL and PL/SQL FAQ
    Please provide table structure, sample data and expected output.
    Additionally when you put some code please enclose it between two lines starting with {noformat}{noformat}
    i.e.:
    {noformat}{noformat}
    SELECT ...
    {noformat}{noformat}
    I noticed also that you have posted the same question here: {message:id=10678000}
    If you move your question to another forum please mark the previous question as answered.
    Regards.
    Al
    Edited by: Alberto Faenza on Nov 7, 2012 5:03 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Maybe you are looking for

  • Pandora very unstable--also experience video freeze

    Hi, I could be happy with my iPad if only the Pandora application--Internet radio--would work. Pandora works great on iPhone and iTouch, but cuts out over and over again on iPad. Often it can't get through 1 song. Have had iPad for 1 month, am taking

  • Playback won't work using Mbox as audio device

    We were previously using Premiere Elements (we're on version 9.0.1) with a Digidesign 002 as the audio interface and everything worked fine.  We have since moved that computer to another workstation with an Mbox Pro, and are having issues whenever th

  • DVDs copied to Apple TV only seem to play in small format on Widescreen TV

    Ok when i purchase a film from iTunes Store, it plays on Apple TV and uses the full capacity of the attached flat screen TV. When I use Aimersoft to copy old collections of DVDs to store them on Apple TV they only seem to play back using 60% of the w

  • Theming Options for Web Dynpro ABAP/Java - Custom CSS

    What options are available for creating a custom CSS for WDA?  My project is going towards SAPUI5, and they want to apply the same look and feeavl to their Web Dynpro applications. I know the Theme Designer is an option - but this customer is running

  • Reducing the time in Queues- IE

    Hi all, My problem is ,for picking the message from the sender queue-> IE->Receiver queue ,to pick the message from the sender queue and place in the Integration engine much time is wasted.Is there any way to decrease this time. Kindly let me know. T