SQL Select grouping by value

Hi ,
I have an output after a join statement and i have output
which is like below.
I have an output containing two colums with same data except for the emp_depts and would like to write a select statement which will give a single row as opposed to two rows as below.
Current out put view.
emp_no emp_Name emp_Depts
1 Test HR
1 Test Finance
Desired output.
1 Test HR,Finance
Can some one please help.

If you are on 10g:
SQL> create table mytable
  2  as
  3  select 1 emp_no, 'Test' emp_Name, 'HR' emp_Depts from dual union all
  4  select 1, 'Test', 'Finance' from dual
  5  /
Tabel is aangemaakt.
SQL> with t as
  2  ( select emp_no,emp_name,substr(ed,2) emp_depts,rn
  3      from mytable
  4     model
  5           partition by (emp_no,emp_name)
  6           dimension by (row_number() over (partition by emp_no order by null) rn)
  7           measures (cast(emp_depts as varchar2(50)) ed)
  8           rules
  9           ( ed[any] order by rn desc = ed[cv()+1] || ',' || ed[cv()]
10           )
11  )
12  select emp_no
13       , emp_name
14       , emp_depts
15    from t
16   where rn = 1
17  /
    EMP_NO EMP_ EMP_DEPTS
         1 Test HR,Finance
1 rij is geselecteerd.And this technique is not called pivoting, but string aggregation. If you search for this term you will see some other techniques as well.
Regards,
Rob.

Similar Messages

  • SQL SELECT and Return values

    I have a simple SELECT statement in a Stored Proc that
    queries 2 tables in
    SQL Server 2000 using a join. My problem is I want to return
    values based on
    the outcome of the query eg. if a row is returned then return
    1, or return 2
    if no rows are found. I have tried various ways to implement
    this and they
    always work fine in Query Analyser but in Dreamweaver MX2004
    either return
    no return values ( or at least they are not displayed on my
    page ) or the
    return values are inconsistent. I am using a DW Command.
    I know this is DW and not my code as Query Analyser produces
    the correct
    values every time. Can anyone tell me what code I can use
    that is DW
    friendly please.
    Thanks.

    I read your note with a great degree of sympathy but no
    solutions. I was having the same trouble with access tables after 4
    years of trouble free connections. It may not be relevant but the
    solutions for my problem was a Server software SP release. Details
    are:
    Following the avalanche of hints and tips I am pleased to
    report that there is a problem with Dreamweaver MX and MX2004 when
    connecting to Access databases running on Windows Server 2003 SP1.
    Whilst it is covered in the knowledge base I note that others in
    the forum are having similar difficulties with Sequel connections
    and unable to retrieve failures.
    There is a "fix" - SP2DBFix1.0.2.mxp - for my problem and I
    cannot be alone in experiencing this.
    And just in case anyone from Adobe is reading this, you
    should have written to all your registered and paid up members
    informing us of this problem when WS2003 SP1 was released!
    Hope this helps.

  • SQL select group by Query

    Suppose a table T1 has 2 columns C1 and C2 and data as follows:
    C1 C2
    == ==
    1 A1
    1 A2
    2 B1
    2 B2
    2 B3
    3 C1
    4 D1
    4 D2
    I want to write a SQL query to select data and display as follows (i.e. grouped by C1, but C2 should display as single field with say '-' seperator):
    C1 C2-Details
    == ========
    1 A1-A2
    2 B1-B2-B3
    3 C1
    4 D1-D2
    Please help.
    Thanks in advance
    Goli

    Your query maybe like this
    SELECT c1,
    LTRIM(MAX(SYS_CONNECT_BY_PATH(c2,'-'))
    KEEP (DENSE_RANK LAST ORDER BY curr),'-') AS employees
    FROM (SELECT c1,
    c2,
    ROW_NUMBER() OVER (PARTITION BY c1 ORDER BY c2) AS curr,
    ROW_NUMBER() OVER (PARTITION BY c1 ORDER BY c2) -1 AS prev
    FROM t1)
    GROUP BY c1
    CONNECT BY prev = PRIOR curr AND c1 = PRIOR c1
    START WITH curr = 1;

  • SQL selecting group by but....

    select count(empno) as total,dept from emp group by dept
    total | dept
    4 | 10
    3 | 20
    5 | 30
    i want an another field with this to give me like this
    employee_ _ Number|total | dept
    121,122,123,124_ _ | 4 | 10
    125,126,127_ _ _ _ | 3 | 20
    129,130,131,132,133| 5 | 30
    means i need list of empno seperated by comma with each group

    Hi,
    Fakhr-e-Alam wrote:
    is there any built in function which give me
    1-upto-4, 11-upto-14, 33,35, 40-upto-41....
    instead of
    1,2,3,4, 11,12,13,14, 33,35, 40,41...There's no single function that will take a set of 12 rows (or a list of 12 comma-separated items) such as 1,2,3,4, 11,12,13,14, 33,35, 40,41 and return a set of 5 rows (or items) like 1-upto-4, 11-upto-14, 33,35, 40-upto-41.
    You can do it with various combinations of anlayitic and aggreagate functions.
    If you'd like help, post CREATE TABLE and INSERT statements for your sample data, and the results you want from that data. Say whatever you know about the starting data. (For example, are the numbers distinct? Are they always integers?)
    If you can show your problem using commonly available tables (like those in the scott schema), then you don't have to post any sample data; just the results, and an explanation of how you get those results from that data.
    Always say which version of Oracle you're using.

  • How can i select other column values('-' separated) in group by function

    CREATE TABLE EMP (
         EMPNO NUMBER(4) NOT NULL,
         ENAME VARCHAR2(10),
         JOB VARCHAR2(9),
         SAL NUMBER(7)
    INSERT INTO EMP(EMPNO, ENAME, JOB, SAL) VALUES (7369, 'SMITH', 'CLERK', 800);
    INSERT INTO EMP(EMPNO, ENAME, JOB, SAL) VALUES (7499, 'SMITH', 'SALESMAN', 1600);
    INSERT INTO EMP(EMPNO, ENAME, JOB, SAL) VALUES (7521, 'ALLEN', 'SALESMAN', 2400);
    In Output I want 3 columns : EMP,SUM(SAL),JOB(hyphenSeparated)
    Means i want my output like
    First row : SMITH,2400,CLERK-SALESMAN
    Second row : ALLEN,2400,SALESMAN
    I tried to write following sql : select ename,sum(sal) from emp group by ename
    But i want other colummn value in '-' separated. but group by is only allowing agreegated function.
    How can i select other column value using group by function.

    SQL>  select ename,sum(sal), listagg(job, '-') within group (order by job) as job  from emp group by ename;
    ENAME        SUM(SAL) JOB
    ALLEN            2400 SALESMAN
    SMITH            2400 CLERK-SALESMANnote: LISTAGG is a feature of 11.2

  • How to exit from SQL*Plus based on the return value of a SQL select stment?

    Hi
    I have a SQL script executed from SQL*Plus. I would like to know if SQL*Plus
    supports any kind of branching or exiting from script execution based on a
    returned value of a SQL select statement. I am on 9i.
    Regards,
    Tamas Szecsy

    in sqlplus, you have whenever
    ex:
    whenever sqlerror exit failure
    insert into ...
    -- if this fails, then you will be out
    insert into ...
    -- if this fails, then you will be out
    whenever sqlerror continue
    insert into ...
    -- if this fails, this continues
    insert into ...and you have PL/SQL
    declare x number;
    begin
    select count(*) into x from emp;
    if (x=14) then null; end if;
    end;
    /note that you can mix those in some case
    -- exit if there is no row in emp
    whenever sqlerror exit 1
    var dummy number
    exec select count(*) into :dummy from emp having count(*)!=0

  • REP-2103: PL/SQL formula returned invalid value or no value

    Hi all,
    Before clone the report GEPS FA Prepare Mass Addition Report runs fine. But after cloned from production, it shows error:
    Enter Password:
    REP-0004: Warning: Unable to open user preference file.
    REP-2103: Column 'G_MASS_ADDITION_INVOICE' : PL/SQL formula returned invalid value or no value.
    REP-0069: Internal error
    REP-57054: In-process job terminated:Terminated with error:
    REP-2103: Column 'G_MASS_ADDITION_INVOICE' : PL/SQL formula returned invalid value or no value.
    Actually I didn't have such column 'G_MASS_ADDITION_INVOICE'. This is a group name. I tried a lot, such as widden the formula column width or delete the formula column, but it still show this error. I didn't have any trigger or other formula column. Even I create a new report use the same name with a single query( select sysdate from dual), but it still shows the same error.
    Is there anyone faced this issue before or can help me ? Thanks
    ares
    Edited by: 930967 on Jun 13, 2012 12:46 AM

    Hi;
    Please see:R12 GL Account Balances Across Ledgers (GLRGCBGT) Report Errors With: REP-0004 MSG-00025 MSG-00104 MSG-01221 REP-2103 REP-0069 REP-57054, Program Exited With Status 1 [ID 1316929.1]
    Regard
    Helios

  • Oracle's SQL select should have a syntax like this

    Oracles SQL is very powerful but lacks a basic thing the LIMIT and OFFSET syntax which
    is present in postgres sql, I recommend oracle to support this syntax in future release.
    more details of this syntax can be found at :
    http://www.postgresql.org/idocs/index.php?sql-select.html
    SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
    * | expression [ AS output_name ] [, ...]
    [ FROM from_item [, ...] ]
    [ WHERE condition ]
    [ GROUP BY expression [, ...] ]
    [ HAVING condition [, ...] ]
    [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
    [ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]
    [ FOR UPDATE [ OF tablename [, ...] ] ]
    [ LIMIT { count | ALL } ]
    [ OFFSET start ]

    I've executed the above queries against my table and the results follows :
    The structure of the table
    Name Null? Type
    FATWAID NOT NULL NUMBER(11)
    FATWASTATUS NUMBER(1)
    SUBJ_NO NUMBER(4)
    LANG CHAR(1)
    SCHOLAR NUMBER(2)
    REVIEWER NUMBER(2)
    AUDITOR NUMBER(2)
    PUBLISHER NUMBER(2)
    SUBSCRIBER NUMBER(2)
    ENTRY NUMBER(2)
    FATWATITLE VARCHAR2(100)
    FATWATEXT CLOB
    FATWADATE DATE
    TRANSLATE CHAR(1)
    SELECTED NUMBER(1)
    SUBJ_MAIN NUMBER(4)
    I have set timing on and executed each query above, following are the statistics.
    First the number of rows in the table.
    SQL>set timing on
    SQL>select count (*) from fatwa;
    COUNT(*)
    1179651
    Elapsed: 00:00:13.05
    The query of Andrew Clarke
    SQL>SELECT fatwaid FROM (SELECT fatwaid, rownum as ranking FROM fatwa) r
    WHERE r.ranking BETWEEN &OFFSET AND &LIMIT
    SQL>/
    Enter value for offset: 100000
    Enter value for limit: 100009
    old 2: WHERE r.ranking BETWEEN &OFFSET AND &LIMIT
    new 2: WHERE r.ranking BETWEEN 100000 AND 100009
    FATWAID
    96592
    96593
    96594
    96595
    96596
    96597
    96598
    96599
    96600
    96601
    10 rows selected.
    Elapsed: 00:00:12.02
    SQL> /
    Enter value for offset: 1000000
    Enter value for limit: 1000009
    old 2: WHERE r.ranking BETWEEN &OFFSET AND &LIMIT
    new 2: WHERE r.ranking BETWEEN 1000000 AND 1000009
    FATWAID
    994621
    994622
    994623
    994624
    994625
    994626
    995769
    995770
    995771
    995772
    10 rows selected.
    Elapsed: 00:00:12.00
    The response time is decreasing because of use of bind variables,
    but 12 seconds is a sign of poor performance.
    Now a slight modification to Clarke's query
    I will add order by clause
    SQL> ed
    Wrote file afiedt.buf
    1 SELECT fatwaid FROM (SELECT fatwaid, rownum as ranking FROM fatwa ORDER BY fatwaid) r
    2* WHERE r.ranking BETWEEN &OFFSET AND &LIMIT
    SQL> /
    Enter value for offset: 100001
    Enter value for limit: 100010
    old 2: WHERE r.ranking BETWEEN &OFFSET AND &LIMIT
    new 2: WHERE r.ranking BETWEEN 100001 AND 100010
    FATWAID
    100032
    100033
    100034
    100035
    100036
    100037
    100038
    100039
    100040
    100041
    10 rows selected.
    Elapsed: 00:00:04.00 -- time reduced from 12 to 4 seconds
    A time of 4 seconds is acceptable but not good,
    response time should be in milli seconds.
    SQL> /
    Enter value for offset: 1000001
    Enter value for limit: 1000010
    old 2: WHERE r.ranking BETWEEN &OFFSET AND &LIMIT
    new 2: WHERE r.ranking BETWEEN 1000001 AND 1000010
    FATWAID
    1000032
    1000033
    1000034
    1000035
    1000036
    1000037
    1000038
    1000039
    1000040
    1000041
    10 rows selected.
    Elapsed: 00:00:03.09 -- this reduction is because of bind variables
    The query of Chris Gates
    SQL>select fatwaid
    2 from ( select a.*, rownum r
    3 from ( select *
    4 from fatwa
    5 --where x = :host_variable
    6 order by fatwaid ) a
    7 where rownum < &HigerBound )
    8 where r > &LowerBound
    SQL> /
    Enter value for higerbound: 100011
    old 7: where rownum < &HigerBound )
    new 7: where rownum < 100011 )
    Enter value for lowerbound: 100000
    old 8: where r > &LowerBound
    new 8: where r > 100000
    FATWAID
    100032
    100033
    100034
    100035
    100036
    100037
    100038
    100039
    100040
    100041
    10 rows selected.
    Elapsed: 00:00:02.04
    This seems to be fast
    SQL> /
    Enter value for higerbound: 1000011
    old 7: where rownum < &HigerBound )
    new 7: where rownum < 1000011 )
    Enter value for lowerbound: 1000000
    old 8: where r > &LowerBound
    new 8: where r > 1000000
    FATWAID
    1000032
    1000033
    1000034
    1000035
    1000036
    1000037
    1000038
    1000039
    1000040
    1000041
    10 rows selected.
    Elapsed: 00:01:14.02
    but this is worst when upper bound is 1 million.
    Finally Myers query
    SQL> select fatwaid from
    2 (select /*+ INDEX(fawtaid pk_fatwa) */ fatwaid, rownum x from fatwa
    3 where rownum < &UpperBound )
    4 where x > &LowerBound;
    Enter value for upperbound: 100011
    old 3: where rownum < &UpperBound )
    new 3: where rownum < 100011 )
    Enter value for lowerbound: 100000
    old 4: where x > &LowerBound
    new 4: where x > 100000
    FATWAID
    122418
    122419
    122420
    122421
    122422
    122423
    122424
    122425
    122426
    122427
    10 rows selected.
    Elapsed: 00:00:00.03 -- too fast
    SQL> /
    Enter value for upperbound: 1000011
    old 3: where rownum < &UpperBound )
    new 3: where rownum < 1000011 )
    Enter value for lowerbound: 1000000
    old 4: where x > &LowerBound
    new 4: where x > 1000000
    FATWAID
    984211
    984212
    984213
    984214
    984215
    984216
    984217
    984218
    984219
    984220
    10 rows selected.
    Elapsed: 00:00:02.02 -- with 1 million rows also satisfactory but it is not is milliseconds
    The same query after using order by clause
    SQL> select fatwaid from
    2 (select /*+ INDEX(fawtaid pk_fatwa) */ fatwaid, rownum x from fatwa
    3 where rownum < &UpperBound ORDER BY fatwaid)
    4 where x > &LowerBound;
    Enter value for upperbound: 100011
    old 3: where rownum < &UpperBound ORDER BY fatwaid)
    new 3: where rownum < 100011 ORDER BY fatwaid)
    Enter value for lowerbound: 100000
    old 4: where x > &LowerBound
    new 4: where x > 100000
    FATWAID
    100032
    100033
    100034
    100035
    100036
    100037
    100038
    100039
    100040
    100041
    10 rows selected.
    Elapsed: 00:00:00.06
    SQL> /
    Enter value for upperbound: 1000011
    old 3: where rownum < &UpperBound ORDER BY fatwaid)
    new 3: where rownum < 1000011 ORDER BY fatwaid)
    Enter value for lowerbound: 1000000
    old 4: where x > &LowerBound
    new 4: where x > 1000000
    FATWAID
    1000032
    1000033
    1000034
    1000035
    1000036
    1000037
    1000038
    1000039
    1000040
    1000041
    10 rows selected.
    Elapsed: 00:00:07.03 -- slow
    SQL> /
    Enter value for upperbound: 1000011
    old 3: where rownum < &UpperBound ORDER BY fatwaid)
    new 3: where rownum < 1000011 ORDER BY fatwaid)
    Enter value for lowerbound: 1000000
    old 4: where x > &LowerBound
    new 4: where x > 1000000
    FATWAID
    1000032
    1000033
    1000034
    1000035
    1000036
    1000037
    1000038
    1000039
    1000040
    1000041
    10 rows selected.
    Elapsed: 00:00:00.06
    when I execute the same query again it is bringing records from
    the SGA so it is very fast
    Now which one to choose from ?
    Andrew and Myers queries are good and currently I am using
    Myers query.
    There should be some technique to do this in the most efficient way.
    any input is appreciated.

  • Querying Data w PL/SQL - selecting 1 row

    Initial research done at:
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/sqloperations.htm#i45320
    Querying Data with PL/SQL - Selecting At Most One Row: SELECT INTO Statement
    I am trying to run a simple SELECT COUNT statement in PL/SQL. Can someone please help me with the syntax? Here is my SQL select count(*) statement.
    SELECT ITEM_TYPE, count(*)
    FROM
    APPLSYS.WF_ITEMS
    WHERE
    ITEM_TYPE = 'GLBATCH' AND
    END_DATE is not null
    GROUP BY ITEM_TYPE;
    Thanks, I tried following some examples found on these forum threads but couldn't seem to find one for something so basic as this.

    I am trying to run a simple SELECT COUNT statement in PL/SQL. Can someone please help me with the syntax?in plsql you would declare variables to hold the selected values:
    declare
       l_item_type   applsys.wf_items.item_type%type;
       l_cnt         integer;
    begin
         select   item_type, count ( * )
           into   l_item_type, l_cnt
           from   applsys.wf_items
          where   item_type = 'GLBATCH' and end_date is not null
       group by   item_type;
       dbms_output.put_line ('Item_type: ' || l_item_type);
       dbms_output.put_line ('Count: ' || l_cnt);
    end;

  • Aggregate sql select

    Hi,
    I'm in Oracle 11g, need some help writing aggregate sql select. That might be really easy I hope.
    We are doing some web analytics and counting clicks on a company’s web site, so I have to count sums on clicks, visitors and sessions from activity table.
    t_agg_activity( date_key, site_id, segment_id, page_id, visitor_count, views_count, session_count)
    The problem is with the Totals numbers. There are visitors and their segments. Segments are also divided by groups. So I need to count records and distinct visitors and distinct sessions.
    Also I need three  records  for Totals: total all and total in group 1 and total in group 2 with the segment key -1, -2, -3 respectively. No Totals for group 3 needed.
    Not sure how to deal with those totals. Should I use Roll Up function or analytic SUM function may be?
    create table t_sites(site_id  NUMBER, site_code VARCHAR2(50));
    insert into t_sites(site_id  , site_code )
    values( 1, 'site_1');
    insert into t_sites(site_id  , site_code )
    values( 2, 'site_2');
    insert into t_sites(site_id  , site_code )
    values( 3, 'site_3');
    create table t_segments(segment_id  NUMBER, segment_group VARCHAR2(50), segment_code VARCHAR2(50));
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(1, 'seg_group_1', 'AAA');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(2, 'seg_group_1', 'BBB');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(3, 'seg_group_1', 'CCC');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(4, 'seg_group_2', 'DDD');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(5, 'seg_group_2', 'EEE');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(6, 'seg_group_2', 'FFF');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(7, 'seg_group_3', 'GGG');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(8, 'seg_group_3', 'HHH');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(9, 'seg_group_3', 'III');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(-1, 'Total All', 'Total');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(-2, 'Total seg_group_1', 'Total G1');
    insert into t_segments(segment_id  , segment_group , segment_code )
    values(-3, 'Total seg_group_2', 'Total G2');
    create table t_activity( date_key NUMBER, page_id NUMBER, site_id  NUMBER, segment_id NUMBER, visitor_id NUMBER, session_id NUMBER);
    insert into t_activity( date_key, page_id, site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 1, 1, 1);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 1, 1, 1);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 1, 1, 2);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 1, 2, 2);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 1, 2, 2);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 1, 2, 22);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 4, 2, 10);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 4, 2, 10);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 4, 2, 10);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 7, 3, 100);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 7, 3, 100);
    insert into t_activity( date_key, page_id,site_id, segment_id, visitor_id, session_id)
    values( 2456141, 1, 1, 7,  3, 100);
    create table t_agg_activity( date_key NUMBER, site_id NUMBER, segment_id NUMBER,  page_id NUMBER, visitor_count NUMBER, views_count NUMBER, session_count NUMBER)
    Edited by: 955537 on Aug 28, 2012 11:19 AM

    955537 wrote:
    If you can help with the full query?
    with t as (
               select  nvl2(segment_group,'Total ' || segment_group,'Total All') segment_group,
                       count(*) cnt
                 from  t_activity a,
                       t_segments s
                 where s.segment_id = a.segment_id
                 group by rollup(segment_group)
    select  segment_id,
             count(*)
      from   t_activity
      group by segment_id
    union all
    select  segment_id,
             cnt
       from  t,
             t_segments s
       where s.segment_group = t.segment_group
    SEGMENT_ID   COUNT(*)
             1          6
             4          3
             7          3
            -1         12
            -2          6
            -3          3
    6 rows selected.
    SQL>  SY.

  • How to group these values month by month ?

    Hi,
    I have a nice SQL statement which returns days by days, the values of a device.
    WITH S1 AS
      (SELECT DATE1,
        ROUND(AVG(VALEUR),2) Debit
         FROM EVV_E032
        WHERE DATE1 BETWEEN TO_DATE('01012006000000', 'DDMMYYYYHH24MISS') AND TO_DATE('31122006235959', 'DDMMYYYYHH24MISS')
      AND CLEF_VAR =
        (SELECT CLEF_VAR FROM SITE_DEBIT_RIVIERE WHERE SITE = 'E032'
    GROUP BY date1
    SELECT NULL LINK    ,
      TO_CHAR(n, 'DD.MM'),
      NVL(ROUND(AVG(Debit),2), 0) "Débit"
       FROM
      (SELECT TRUNC(TRUNC(to_date(2006,'YYYY'),'year'), 'DD')-1 + level n,
        rownum jours
         FROM dual CONNECT BY level<=366
      ) days
    LEFT JOIN s1
         ON days.n = TRUNC(date1,'DD')
    GROUP BY n
    ORDER BY nSample values :
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('10/02/2006 09:49:59','DD/MM/YYYY HH24:MI:SS') 1,63);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('21/02/2006 10:35:12','DD/MM/YYYY HH24:MI:SS') 1,68);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('21/02/2006 11:30:30','DD/MM/YYYY HH24:MI:SS') 0);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('23/02/2006 14:02:02','DD/MM/YYYY HH24:MI:SS') 0);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('23/02/2006 16:22:34','DD/MM/YYYY HH24:MI:SS') 0);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('30/04/2006 18:09:08','DD/MM/YYYY HH24:MI:SS') 1,72);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('20/05/2006 11:57:02','DD/MM/YYYY HH24:MI:SS') 1,72);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('07/06/2006 15:11:58','DD/MM/YYYY HH24:MI:SS') 1,79);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('08/06/2006 20:00:26','DD/MM/YYYY HH24:MI:SS') 1,82);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('19/06/2006 09:42:32','DD/MM/YYYY HH24:MI:SS') 1,72);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('20/06/2006 04:30:00','DD/MM/YYYY HH24:MI:SS') 1,82);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('20/06/2006 10:39:01','DD/MM/YYYY HH24:MI:SS') 1,72);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('24/06/2006 19:34:50','DD/MM/YYYY HH24:MI:SS') 1,82);
    Insert into "EVV_E032" (DATE1,DEBIT) values (to_date('26/06/2006 14:37:26','DD/MM/YYYY HH24:MI:SS') 1,88);The output are values grouped day by day. I would like to group tehse values month by month, but could not figure how to do. Even though I am not a newbie newbie on SQL, this code is going far too much for me. I know some of you guys can handle this. I tried hard but coud not succeed. Could you help me ?
    Regards, Christian.

    Difficult to work out (read: too much hassle) with your data and sql, as you haven't provided a set of info for all the tables provided, but hopefully this will give you an idea:
    with my_tab as (select trunc(sysdate)+30 dt, 1 val from dual union all
                    select trunc(sysdate) dt, 2 val from dual union all
                    select trunc(sysdate) dt, 20 val from dual union all
                    select trunc(sysdate)+30 dt, 10 val from dual union all
                    select trunc(sysdate)+60 dt, 6 val from dual)
    -- end of mockup of table "my_tab"; see SQL below...
    select trunc(dt, 'mm') dt, sum(val)
    from   my_tab
    group by trunc(dt, 'mm')
    order by trunc(dt, 'mm');
    DT          SUM(VAL)
    01-MAR-09         22
    01-APR-09         11
    01-MAY-09          6

  • Help with record selection/grouping

    I am trying to create a report as follows, but am running into some problems with my selection/grouping:
    Data Structure (each line is a separate record; each employee has 2 records)
    Employee             TaskID      TaskStatus           Marker         Date Modified
    1                               A                CLOSED             x
    1                               B                OPEN                  N/A
    2                               A                CLOSED              y
    2                               B                OPEN                 N/A
    3                               A                CLOSED             x
    3                               B                CLOSED            N/A
    4                               A                CLOSED             y
    4                               B                CLOSED           N/A
    Report Requirements
    I want a report that displays only employee number and the date that Task B was completed for all employees, and I want these records grouped based on Task B's Task Status (no problem.  I did this).
    However, I want to sub-group these Task B records (within Task Status) by the Marker field for Task A records!  (I can't figure this out?  If I only pull in Task B records, how can I compare what the associated employee has as a marker for their Task A record?)
    Again, I only want to display data from the employee's Task B record, while subgrouping on a field value from the empoyee's Task A record.  The report would be structured as follows:
    Task B (OPEN), with Task A - Marker (x)
                     {Date Modified}
    Task B (OPEN), with Task A - Marker (y)
                     {Date Modified}
    Task B (CLOSED), with Task A - Marker (x)
                    {Date Modified}
    Task B (CLOSED), with Task A - Marker (y)
                      {Date Modified}
    Thanks.
    Gary

    The easiest way would be to use an SQL Command that returns both the Task A and Task B data on one record.  Something like (MS SQL):
    select b.employee, b.taskid as task_b, b.task_status_b, b.marker as marker_b, b.date_modified as date_modified_b,
        a.taskid as task_a, a.task_status_a, a.marker as marker_a, a.date_modified as date_modified_a
    from table a, table b
    where a.employee = b.employee
    and a.taskid = 'A'
    and b.taskid = 'B'
    HTH,
    Carl

  • SQL to group by n days

    i would like know how i can perform grouping based on every n day interval.
    for example, i want to group the data based on an n=2day interval
    example table:
    DATE VALUE
    12/12/2005 2:52:00 PM 10
    12/12/2005 4:49:00 PM 20
    13/12/2005 5:17:00 AM 30
    13/12/2005 5:20:00 AM 40
    14/12/2005 9:36:00 AM 50
    14/12/2005 9:49:00 AM 60
    expected result:
    STARTDATE ENDDATE SUM(VALUE)
    12/12/2005 13/12/2005 100
    14/12/2005 15/12/2005 110
    but i also want to be able to change the value of n (in days)
    Thanks in advance.
    Simon

    something like...
    SQL> def n = 1
    SQL> select min(hiredate) start_date, min(hiredate)+&&n end_date, sum(sal) from emp
      2  group by hiredate, hiredate+&&n order by 1,2;
    old   1: select min(hiredate) start_date, min(hiredate)+&&n end_date, sum(sal) from emp
    new   1: select min(hiredate) start_date, min(hiredate)+1 end_date, sum(sal) from emp
    old   2: group by hiredate, hiredate+&&n order by 1,2
    new   2: group by hiredate, hiredate+1 order by 1,2
    START_DATE END_DATE     SUM(SAL)
    17/12/1980 18/12/1980        800
    20/02/1981 21/02/1981       1600
    22/02/1981 23/02/1981       1250
    02/04/1981 03/04/1981       2975
    01/05/1981 02/05/1981       2850
    09/06/1981 10/06/1981       2450
    08/09/1981 09/09/1981       1500
    28/09/1981 29/09/1981       1250
    17/11/1981 18/11/1981       5000
    03/12/1981 04/12/1981       3950
    23/01/1982 24/01/1982       1300
    START_DATE END_DATE     SUM(SAL)
    19/04/1987 20/04/1987       3000
    23/05/1987 24/05/1987       1100
    13 rows selected.where n is the 'day interval'

  • Select MULTIPLE default values for a multiple selection list box based on another field in Infopath 2010

    Hello there - Before I explain my issue, I would like to point out that I have reviewed some other discussions on selecting default values for multiple selection listbox. But my issue is specific and different, and not answered by any of the discussions
    I visited.
    I have a multiple selection list box (say for example all countries in the world as values), and I would like to pre-select or setup multiple default values (say five countries) based on some criteria that I query from MS SQL database table.
    I know we can go to Data | Default Values option to setup one or many default values for multiple selection list box. When I enter the default values manually this works. I also right click the field under the Multiple-Selection List Box group, then select
    Add another Value Below and set the Default Value for this field to setup multiple default values.
    However, if I reference a field (either an infopath field or a field from SQL database) I am not able to setup multiple default values. Infopath automatically selects the last field I selected for all instances and in the end I am able to see only one
    default value selected instead of many. How to fix this problem? Why would infopath allow multiple default values when we enter it manually but not when we reference some fields?
    Please let me know if you need more info. Appreciate your help.
    Thanks!

    Hi redhotc,
    According to your description, my understanding is that you want to set multiple default values for a multiple checkbox list in InfoPath form.
    I did a test with SQL database table. I set three default values for the checkbox list by adding three values field under the group field(Data->Default values), each value field is for a default value. Then publish it to my SharePoint site, everything
    was fine.Please have a try as the below link:
    http://www.bizsupportonline.net/infopath2010/pre-select-items-multiple-selection-list-box-infopath-2010.htm
    Note: if you are using SQL databse table, you may need to enable ‘Allow cross-domain data access for user form templates that use connection settings in a data connection file’ in CA. More information, please refer to:
    http://answers.flyppdevportal.com/categories/sharepoint2010/sharepoint2010customization.aspx?ID=418b9423-a96c-4e5e-91f9-6a1b010ebb69
    I hope this helps.
    Thanks,
    Wendy
    Wendy Li
    TechNet Community Support

  • Help with sql subquery/grouping

    Hi... having trouble getting this query to work. Here's the table and data:
    create table mytable (
    rec_num number,
    status_num number,
    status_date date
    insert into mytable values (1,1,'01-AUG-2006');
    insert into mytable values (1,2,'14-AUG-2006');
    insert into mytable values (1,8,'01-SEP-2006');
    insert into mytable values (1,3,'15-SEP-2006');
    insert into mytable values (1,2,'03-SEP-2006');
    insert into mytable values (2,2,'17-AUG-2006');
    insert into mytable values (3,2,'02-SEP-2006');
    insert into mytable values (3,4,'07-SEP-2006');
    insert into mytable values (4,1,'18-SEP-2006');
    insert into mytable values (4,4,'27-SEP-2006');
    insert into mytable values (4,2,'18-SEP-2006');
    insert into mytable values (5,1,'01-OCT-2006');
    insert into mytable values (5,2,'03-OCT-2006');
    insert into mytable values (5,3,'05-OCT-2006');
    insert into mytable values (6,1,'01-OCT-2006');
    insert into mytable values (7,2,'14-OCT-2006');
    insert into mytable values (7,8,'15-OCT-2006');
    I'm trying to select the rec_num, status_num, and status_date for the max date for each individual rec_num... it basically tells me the current status of each individual record by getting the status for the most recent date... like the following:
    rec_num, status_num, date
    1, 3, 15-SEP-2006
    2, 2, 17-AUG-2006
    3, 4, 07-SEP-2006
    etc
    This query works... it gets me the max date for each record... but it doesn't give me the status_num:
    select rec_num, max(status_date)
    from mytable
    group by rec_num
    Adding status_num messes it up... I know I need some kindof sub-query, but haven't been able to get one to work.
    I'd also like a query to get a count on how many records currently exist in each status.
    Can someone help me out? Oracle 8i. Thanks!

    SQL> select * from my_table;
       REC_NUM STATUS_NUM STATUS_DA
             1          1 01-AUG-06
             1          2 14-AUG-06
             1          8 01-SEP-06
             1          3 15-SEP-06
             1          2 03-SEP-06
             2          2 17-AUG-06
             3          2 02-SEP-06
             3          4 07-SEP-06
             4          1 18-SEP-06
             4          4 27-SEP-06
             4          2 18-SEP-06
             5          1 01-OCT-06
             5          2 03-OCT-06
             5          3 05-OCT-06
             6          1 01-OCT-06
             7          2 14-OCT-06
             7          8 15-OCT-06
    SQL> select mt.rec_num,
      2         mt.status_num,
      3         mt.status_date
      4    from (select row_number() over (partition by rec_num order by status_date desc) rn,
      5                 rec_num,
      6                 status_num,
      7                 status_date
      8            from my_table) mt
      9   where mt.rn = 1;
       REC_NUM STATUS_NUM STATUS_DA
             1          3 15-SEP-06
             2          2 17-AUG-06
             3          4 07-SEP-06
             4          4 27-SEP-06
             5          3 05-OCT-06
             6          1 01-OCT-06
             7          8 15-OCT-06
    7 rows selected.
    SQL>

Maybe you are looking for