Group by Having Count(*)

Hi...
I'm back again and I'm ever hopeful that I will get some needed help.
I'm trying to use the groupby and having Count(*) on a table, but I think
I have my group by wrong...I keep getting the wrong output.
Here's my code:
Select Course_ID,Count(*),Department_ID,Class_ID,Class_Room
from Class
Group By Course_ID,Department_ID,Class_ID,Class_Room
Having Count(*) >=1
and this is the output:
COURS COUNT(*) DEPARTMENT_ID CLASS_ID CLASS_ROOM
50 1 MATH 103600 140
101 1 BIO 109100 180
101 1 ENGL 108300 150
115 1 HIST 108400 200
183 1 PSYCH 103400 150
183 1 PSYCH 108600 240
184 1 HIST 104500 200
198 1 PHILO 104200 150
199 1 ENG 110300 255
199 1 ECON 120200 235
Now, what should be happeing is that Count should give me 2*101,2*183,2*199 but I keep
getting 1 of each...
Any ideas about where I'm going Wrong?

Drake,
In short, you are grouping on too many things. You need to group on only course_id when looking for duplicates, and then join the duplicate courses back to the course table to get the rest of the data:
select c.course_id, c.department_id,
c.class_id, c.class_room
from class c,
(select course_id, count(*) howmany
from class
group by course_id
having count(*) > 1) dups
where c.course_id = dups.course_id;
Hi...
I'm back again and I'm ever hopeful that I will get some needed help.
I'm trying to use the groupby and having Count(*) on a table, but I think
I have my group by wrong...I keep getting the wrong output.
Here's my code:
Select Course_ID,Count(*),Department_ID,Class_ID,Class_Room
from Class
Group By Course_ID,Department_ID,Class_ID,Class_Room
Having Count(*) >=1
and this is the output:
COURS COUNT(*) DEPARTMENT_ID CLASS_ID CLASS_ROOM
50 1 MATH 103600 140
101 1 BIO 109100 180
101 1 ENGL 108300 150
115 1 HIST 108400 200
183 1 PSYCH 103400 150
183 1 PSYCH 108600 240
184 1 HIST 104500 200
198 1 PHILO 104200 150
199 1 ENG 110300 255
199 1 ECON 120200 235
Now, what should be happeing is that Count should give me 2*101,2*183,2*199 but I keep
getting 1 of each...
Any ideas about where I'm going Wrong?

Similar Messages

  • Group by having count error

    I have this code with an error:
    SELECT (CASE id_type
    WHEN 4 THEN companyid
    ELSE nric
    END) AS customerid
    FROM customer
    GROUP BY customerid HAVING COUNT (*customerid*) = 1
    The error is at the bold word with the error: Invalid identifier. How should I rewrite my sql statement?

    SELECT CUSTOMERID FROM
    SELECT (CASE id_type
    WHEN 4 THEN companyid
    ELSE nric
    END) AS customerid
    FROM customer
    GROUP BY customerid HAVING COUNT (customerid) = 1

  • Count(field) group by having changed to an analytic function

    Goal: Be able to show columns with data not in the group by clause.
    I couldn't find this on the 'net either by looking here or by googling for it (unless I'm looking for the wrong thing). I'm doing a regular:
    select fielda, count(fielda)
    from some_table
    group by fielda1
    having count(field1) > 1;
    to find out where I have duplicates in a table (fielda). I get fielda and the count of those records back (ie X, 2). However, I want to add several more fields that may or may not be different for each of those records.
    For example:
    1 record has fielda = X, field2 = Test, field3 = Oracle, field4 = Unix
    1 record has fielda = X, field2 = Testa, field3 = Sybase, field4 = Windows.
    I want to be able to have the following output:
    X, 2 (count of records with fieldA = X), Test, Oracle, Unix
    X, 2 (count of records with fieldA = X), Testa, Sybase, Windows
    I'm not sure if this is one of those where I have to do a subquery on or not. Any ideas?
    Thanks as always!
    Vic

    Sorry, I misread your statement - it is poorly formatted. Besides incorrect use of aliases you are referencing columns that are not in inline view, therefore you must include it there:
    select  nsn,
            item_type_name,
            item_type_id,
            date_time_added,
            function_name
      from  (
             select  it.nsn,
                     it.item_type_name,
                     it.item_type_id,
                     it.date_time_added,
                     fn.function_name,
                     count(*) over (partition by it.nsn) cnt,
                     it.item_type_cat_id,
                     ic.item_category_id,
                     ic.function_id ic_function_id,
                     fn.function_id fn_function_id
               from  item_types it,
                     item_categories ic, functions fn
               where it.nsn is not null
                 and it.item_type_cat_id = ic.item_category_id
                 and ic.function_id = fn.function_id
      where cnt > 1
        and nsn is not null
        and item_type_cat_id = item_category_id
        and ic_function_id = fn_function_id
    order by nsn;SY.

  • Select query having count function is returning null

    Hi Everyone,
    Please see the query and help me to get this solved.
    DB version
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
    PL/SQL Release 10.2.0.1.0 - Production
    "CORE     10.2.0.1.0     Production"
    TNS for Linux: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - ProductionQUERY
    SELECT COUNT(*) as V_COUNT
          FROM LAB_ORDER_TMPLT_TEST
          WHERE TEST_ID IN
          (SELECT WEST_TEST_ID
                  FROM TEST_CODE_CONVERSION TCC
                  WHERE TCC.EAST_TEST_ID = 2769)
          GROUP BY LAB_ORDER_TMPLT_ID
          HAVING COUNT(*) > 1;
    It's returning null. Is there any way I can get the value as zero instead of null? Please help.Regards,
    BS2012.

    Hi,
    BS2012 wrote:
    Hi Everyone,
    Please see the query and help me to get this solved.
    DB version
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
    PL/SQL Release 10.2.0.1.0 - Production
    "CORE     10.2.0.1.0     Production"
    TNS for Linux: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - ProductionQUERY
    SELECT COUNT(*) as V_COUNT
    FROM LAB_ORDER_TMPLT_TEST
    WHERE TEST_ID IN
    (SELECT WEST_TEST_ID
    FROM TEST_CODE_CONVERSION TCC
    WHERE TCC.EAST_TEST_ID = 2769)
    GROUP BY LAB_ORDER_TMPLT_ID
    HAVING COUNT(*) > 1;
    It's returning null.
    Really? COUNT never returns NULL; it always returns 0 or a positive integer.
    Do you mean the query results in "no rows selected"?
    Is there any way I can get the value as zero instead of null? Please help.Do you want a query that will produce the same output as the query above, but will produce a single row containing 0 when the query above does not produce anything? That sounds like a job for an outer join:
    WITH    original_query  AS
           SELECT  COUNT(*) AS v_count
           FROM    lab_order_tmplt_test
           WHERE   test_id IN
                       (   SELECT  west_test_id
                           FROM        test_code_conversion tcc
                           WHERE   tcc.east_test_id = 2769
           GROUP BY  lab_order_tmplt_id
    SELECT     NVL (o.v_count, 0)     AS v_count
    FROM               dual
    LEFT OUTER JOIN  original_query  o  ON  o.v_count > 0
    ;If you'd care to post CREATE TABLE and INSERT statements for the sample data, then I could test this.

  • Group by having clause problem

    Hi,
    I have a query like this:
    select a.id,a.first_nm,a.last_nm,a.subgroup_id
    from temp a
    where (a.subgroup_id like 'D%' or a.subgroup_id like 'G%')
    group by a.id,a.first_nm,a.last_nm,a.subgroup_id having
    count(*) >1
    union
    select b.id,b.first_nm,b.last_nm,b.subgroup_id
    from temp b
    where b. subgroup_id like 'F%'
    group by b.id,b.first_nm,b.last_nm,b.subgroup_id having count(*) > 2;
    I want the result like this:
    10001 Wendy Lin DAP
    10001 Wendy Lin GMP
    10002 Jim Davis FAD
    10002 Jim Davis FIP
    10002 Jim Davis FIAP
    My query above didn't give the right result, Can someone help me?
    Thank you very much!
    Wendy

    You may like to try this way...
    select id,name,sub_id
    from a
    where id in (select id
    from a
    group by id
    having count(id)>1)
    order by id,name, sub_id asc;
    results are..
    ID NAME CODE
    1 wendy DIA
    1 wendy LCA
    1 wendy PLA
    2 jim CPR
    2 jim DIA
    2 jim PLA
    Hope this helps.

  • Having count- count(count(1))

    I have two queries below- first of which outputs one record, the Second query doesn't.
    Can you explain why First query returns always records, at least one record? In explanation, can you say which process is runned after which (something like where is runned firstly, then group by, then...)?
    --First:
    select count(count(1)) from dual
    where 1=2
    group by dummy
    having count(dummy) > 0;--1 row, value: 0
    --Second:
    select count(1) from dual
    where 1=2
    group by dummy
    having count(dummy) > 0;--0 rows

    Hi,
    Aggregate queries with nested aggregate functions (such as "COUNT (COUNT (1))") always return exactly 1 row, even if there are no rows in the table (or, as in your first example, no rows survive the WHERE clause).
    The outer layer of nested aggreate functions is evaluated after the GROUP BY and HAVING clauses have been applied.
    If you have a GROUP BY clause, but no nested aggregate fucntions, then the output contains one row for every distinct group, subject to the WHERE- and HAVING clauses. In your second example, no rows are left after the WHERE clause, so there will be no rows in the output.
    You can think of things as happening in this order
    (1) WHERE
    (2) GROUP BY
    (3) (inner) aggregate fucntions
    (4) HAVING
    (5) Nested aggregate functions
    The optimizer may not actually follow the steps above, but you won't notice that.
    Edited by: Frank Kulash on Mar 25, 2010 11:51 AM
    The following thread may help you:
    Re: group function

  • Another group by having problem I think ...

    I have a table defined like so ...
    create table mytable (
    subcode number,
    name varchar2(10)
    I suspect that I have records in this table that look like so ...
    SUBCODE NAME
    123 Jim
    123 Mary
    ... this is bad.
    I ran a query like this ...
    select subcode, name, count(subcode) from mytable group by subcode, name having count(subcode) > 1
    ... and got back zero results.
    Does this mean I DO NOT have duplicates?
    Thanks

    You do have duplicates but your statement doesn't show that since you are grouping by SUBCODE and NAME. Since your have the same SUBCODE but a different NAME for the duplicate SUBCODE you get only 1 result with your group by statement. Here are two examples:
    select subcode, name, count(subcode)
    from mytable
    group by subcode, name
       SUBCODE COUNT(SUBCODE)
           123              2
    select subcode, name, count(subcode)
    from mytable
    group by subcode, name
       SUBCODE NAME       COUNT(SUBCODE)
           123 JIM                     1
           123 MARY                    1This statement will give you what you are looking for:
    select subcode, name
    from mytable
    where subcode = (select subcode
                        from mytable
                        group by subcode
                        having count(subcode) > 1);

  • Create Table  From a Having Count Select

    Hello Folks, any ideas here, I need to create a table using the below 'Select' as the criteria.
    I have the following table and its has duplicate records, identified by double Unique_seq. I want to create a table of these else where, then delete them from schema.tablename.
    Schmea.tablename
    field1
    field2
    field3
    field4
    Unique_Seq
    SELECT Unique_Seq,COUNT(Unique_Seq)
    FROM Schema.Tablename
    GROUP BY Unique_Seq
    HAVING ( COUNT(Unique_Seq) > 1
    I want to create the table using all the fields from schema.tablename
    Thanks

    Thanks for the table script and insert statement.
    here is what you are looking for
    select * from tester_c;
    AD_ID                                    CD_ID                               UNIQUE_SEQ Z_MASTER_KEY
    OP0021889/10                             OP0021889/10                            529832 NNCC00491362OP0021889/1025-JAN-10
                                             OP0021889/10                            529832 NNCC0049136225-JAN-10
                                             OP0022096/10                            539481 NNCC0060037812-JAN-10
    OP0022096/10                             OP0022096/10                            539481 NNCC00600378OP0022096/1012-JAN-10
    LI0192                                   IK735241                                 53925 PP54TH34120988
                                             KL1923836                                65478 BG7892534KL1923836
                                             KL1923836                                65478 BG7892534KL1923836
    PQ8712346                                BN27345236                               64877 7234723642
    8 rows selected.
    Elapsed: 00:00:00.01Pushing duplicate data into a new table tester_c_dup
    CREATE Table tester_c_dup as SELECT * FROM tester_c A WHERE a.rowid > ANY (SELECT  B.rowid FROM
            Tester_c B
         WHERE
            a.Unique_Seq = B.Unique_Seq)
    select * from tester_c_dup;
    AD_ID                                    CD_ID                               UNIQUE_SEQ Z_MASTER_KEY
                                             OP0021889/10                            529832 NNCC0049136225-JAN-10
    OP0022096/10                             OP0022096/10                            539481 NNCC00600378OP0022096/1012-JAN-10
                                             KL1923836                                65478 BG7892534KL1923836
    Elapsed: 00:00:00.01Deleting duplicate data from tester_c table
    DELETE FROM tester_c A WHERE a.rowid > ANY (SELECT  B.rowid FROM
            Tester_c B
         WHERE
            a.Unique_Seq = B.Unique_Seq)
    select * from tester_c;
    AD_ID                                    CD_ID                               UNIQUE_SEQ Z_MASTER_KEY
    OP0021889/10                             OP0021889/10                            529832 NNCC00491362OP0021889/1025-JAN-10
                                             OP0022096/10                            539481 NNCC0060037812-JAN-10
    LI0192                                   IK735241                                 53925 PP54TH34120988
                                             KL1923836                                65478 BG7892534KL1923836
    PQ8712346                                BN27345236                               64877 7234723642
    Elapsed: 00:00:00.00HTH,
    Prazy

  • How to display rows in PSA having count(wbs) 1

    Hi there BW gurus.
    I have a history in the SQL world, where it is quite easy to write a SQL code to get out the rows from a table with duplicates in one spesific field. (Beeing indexed or not)
    I have tried to search for this option where we diplays the content in an PSA. but not found it.
    Anyone having a good solution for this.
    (I am not an ABAP-er)
    Best Regards Ingrid

    Hi Ingrid,
    following ABAP returns all languages which more than one user is assigned to.
    data: l_langu type usr01-langu.
    data: l_Num type i.
    SELECT count( * ) langu
      into (l_Num, l_langu)
    FROM usr01
    GROUP BY langu
       having count( * ) GT 1.
      write: / l_langu, ' - ', l_num.
    endselect.
    e.g. language E is assigned to 27 users.
    Hope this gives you an idea.
    Regards,
    Jürgen

  • Can I write merge SQL statement having count(*)?

    Hi this is a followup question of my previous post. I tried to use a merge SQL statement to solve my problem but now I bump into another problem.
    Now I have a table where the field I need to update is a partial PK
    when using a merge SQL statement, I have to put a where clause that check if count(*) = 1 because of the PK constraint
    Where can I put the count(*) = 1 clause?
    Here are the details:
    I have two tables TA and TB, where TA contains the fields ID, FULLNAME, TYPE and TB contains the fields ID, FIRSTNAME
    I want to update the firstnames in TB to be the firstnames from TA where TB.ID = TA.ID and TA.TYPE = 'ABC'
    {ID, FIRSTNAME} are PKs but for the same ID, there can be more than 1 firstname.
    e.g.
    TA
    ID | FULLNAME | TYPE
    1 Caroline T ABC
    2 Mary C DEF
    3 Peter J ABC
    TB
    ID | FIRSTNAME
    1 Caroline
    1 Carol
    1 C,
    3 Peter
    I need to update TB with the new firstnames from TA where type is 'ABC' but only for those fields that have count(TB.ID) = 1
    when I try to run this SQL statement
    merge into TB B using TA A
    on (A.ID = B.ID and A.TYPE = 'ABC')
    when matched then update set B.FIRSTNAME = substr(A.FULLNAME, 1, instr(A.FULLNAME, ',') - 1)
    I got this error SQL Error: ORA-00001: unique constraint (TEST.PK_TB) violated
    which I believe is because I updated those fields say ID = 1, all with 'Caroline'
    that means I will have to add a clause having count(TB.ID) = 1
    How would you do it?
    Server is Oracle 11g
    Thank you!

    Hi,
    One way is to join ta and tb in the USING clause, and eliminate the duplicates there.
    MERGE INTO     tb     dst
    USING   (
             SELECT    ta.id
             ,           REGEXP_SUBSTR ( MIN (ta.fullname)
                                    , '^[^,]*'
                            )     AS firstname
             FROM      ta
             JOIN      tb  ON  ta.id     = tb.id
             WHERE     ta.type      = 'ABC'
             GROUP BY  ta.id
             HAVING    COUNT (*)     = 1
         )                   src
    ON     (scr.id      = dst.id)
    WHEN MATCHED THEN UPDATE
    SET     dst.firstname     = src.firstname
    ;If you'd care to post CREATE TABLE and INSERT statements for your sample data, then I could test this.
    I used REGEXP_SUBST instead of SUBSTR and INSTR to find the firstname, because I find it a little cleaner, and becuase it returns something even when there is no ',' in fullname, which I'm guessing is what you really want. (None of the fullnames in your sample data have ','s.) You could use SUBSTR and INSTR instead.

  • Having count(*) 1

    with my_table as
       (select '402' id, '75175637' snr from dual union all
        select '400' id, '15208367' snr from dual union all
        select '400' id, '45208312' snr from dual union all
        select '400' id, '55208344' snr from dual union all
        select '402' id, '65208317' snr from dual union all
        select '402' id, '65208317' snr from dual union all
        select '402' id, '75208310' snr from dual union all
        select '402' id, '85208310' snr from dual union all
        select '421' id, '75208310' snr from dual union all
        select '421' id, '99908456' snr from dual union all
        select '422' id, '10008222' snr from dual)     -- end of data sample
    select b.snr
    from (select id, snr
          from my_table
          group by id, snr) b
        group by b.snr
    having count(*) > 1   ;
    SNR
    75208310SNR 75208310 is among ID 402 and 421. The output should be the following:
    ID  SNR
    402 75208310
    421 75208310Does anybody have an idea?

    Thank you @all,
    Warren,
    after I add the statement >>select '402' id, '65208317' snr from dual union all<<
    to the data sample I get the following
    SQL> with my_table as
      2         (select '402' id, '75175637' snr from dual union all
      3          select '400' id, '15208367' snr from dual union all
      4          select '400' id, '45208312' snr from dual union all
      5          select '400' id, '55208344' snr from dual union all
      6          select '402' id, '65208317' snr from dual union all
      7    select '402' id, '65208317' snr from dual union all  -- additional statement
      8          select '402' id, '75208310' snr from dual union all
      9          select '402' id, '85208310' snr from dual union all
    10          select '421' id, '75208310' snr from dual union all
    11         select '421' id, '99908456' snr from dual union all
    12         select '422' id, '10008222' snr from dual) -- end of data sample
    13     select distinct
    14            mt1.id,
    15            mt1.snr
    16       from my_table mt1,
    17            (select snr, count(*) cnt
    18               from my_table
    19             group by snr) mt2
    20      where mt1.snr = mt2.snr
    21        and mt2.cnt > 1;
    ID  SNR
    402 65208317     
    402 75208310
    421 75208310
    SQL>   But I do not need 402, 65208317 in my output.
    I solved it with
    SQL> with my_table as
      2     (select '402' id, '75175637' snr from dual union all
      3          select '400' id, '15208367' snr from dual union all
      4          select '400' id, '45208312' snr from dual union all
      5          select '400' id, '55208344' snr from dual union all
      6          select '402' id, '65208317' snr from dual union all
      7    select '402' id, '65208317' snr from dual union all  -- additional statement
      8          select '402' id, '75208310' snr from dual union all
      9          select '402' id, '85208310' snr from dual union all
    10          select '421' id, '75208310' snr from dual union all
    11         select '421' id, '99908456' snr from dual union all
    12         select '422' id, '10008222' snr from dual) -- end of data sample
    13   select id, snr
    14    from
    15    ( select id, snr, count(*) over (partition by snr) as cnt
    16        from my_table
    17        group by id, snr
    18    )
    19    where cnt > 1
    20   ;
    ID  SNR
    402 75208310
    421 75208310
    SQL>

  • How can I forward a mail to multiple mail accounts that I have in a group without having to add them one by one?

    How can I forward a mail to multiple mail accounts that I have in a group without having to add them one by one?

    I don't think that you can. Yahoo's basic email features do not include mail forwarding.

  • Having count of 1

    I would like to count how many ID have only had 1 Enrolment by year:
    I am assuming you would have to use having count = 1 or something like that.
    So I have the following example below
    ID 1 has 2 enrolments so dont count
    ID 2 has only 1 enrolment so count
    ID 3 has 2 enrolments so dont count
    ID 4 has 1 enrolment so count
    ID 5 has 2 enrolments so dont count
    ID 6 has 1 so count
    ID 7 and 8 have one each so count.
    Thanks
    CREATE TABLE DAN_PRU
    (ID     VARCHAR2(8),
    UNIT    VARCHAR2(8),
    YEAR VARCHAR2(8))
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (1,'POP','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (1,'DOP','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (2,'KUIP','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (3,'DIP','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (3,'YUP','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (4,'SII','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (5,'DYY','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (5,'LIW','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (6,'DFG','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (7,'DWQ','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (8,'DFR','2013');GIVES:
    ID     UNIT     YEAR
    1     POP     2013
    1     DOP     2013
    2     KUIP     2013
    3     DIP     2013
    3     YUP     2013
    4     SII     2013
    5     DYY     2012
    5     LIW     2012
    6     DFG     2012
    7     DWQ     2011
    8     DFR     2011WANT
    ENROLMENTS     YEAR
    2            2013
    1            2012
    2            2011
    using oracle 10

    Hi,
    Interesting problem!
    Chloe_19 wrote:
    I would like to count how many ID have only had 1 Enrolment by year:
    I am assuming you would have to use having count = 1 or something like that.That's an obvious first step. What makes this such an interesting problem is how you get from there to the final answer.
    What is the final answer? Do you want just one row, containing just one number (5 in this case, since there are 5 ids (2, 4, 6, 7 and 8) that have only 1 enrollment?
    So I have the following example below
    ID 1 has 2 enrolments so dont count
    ID 2 has only 1 enrolment so count
    ID 3 has 2 enrolments so dont count
    ID 4 has 1 enrolment so count
    ID 5 has 2 enrolments so dont count
    ID 6 has 1 so count
    ID 7 and 8 have one each so count.
    Thanks
    CREATE TABLE DAN_PRU
    (ID     VARCHAR2(8),
    UNIT    VARCHAR2(8),
    YEAR VARCHAR2(8)) Why is year a VARCHAR2? Why is it 8 characters long?
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (1,'POP','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (1,'DOP','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (2,'KUIP','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (3,'DIP','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (3,'YUP','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (4,'SII','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (5,'DYY','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (5,'LIW','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (6,'DFG','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (7,'DWQ','2013');
    INSERT INTO DAN_PRU (ID, UNIT, YEAR) VALUES (8,'DFR','2013');
    GIVES:How does the data above give the results below.
    For example, it looks like '2013' is the only year in the data. How do you get '2011' and '2012' in the results?
    Can the same id have multiple years? What results would you want in that case?
    ID     UNIT     YEAR
    1     POP     2013
    1     DOP     2013
    2     KUIP     2013
    3     DIP     2013
    3     YUP     2013
    4     SII     2013
    5     DYY     2012
    5     LIW     2012
    6     DFG     2012
    7     DWQ     2011
    8     DFR     2011WANTAre you saying the results below are what you want?
    ENROLMENTS     YEAR
    2            2013
    1            2012
    2            2011
    using oracle 10Can you get a result set that contains one row per id? (Or for each id/year combination; it's unclar what you want.)
    Can you restrict that result set to contain only the ids with only 1 enrollment?
    If you had a table like the previous result set, how would you solve the problem? Remember, any result set can be treated like a table, using a WITH clause.

  • Where, Group By, Having, Order --- Usage sequence?

    Hi
    Can anyone tell me the sequence in which below clauses can be placed:
    Where, Group By, Having, Order by
    Moreover can we use Having after where and before Group By?
    Thanks!! http://gurunguns.wordpress.com

    Here is a good blog explaining the sequence
    http://pratchev.blogspot.com/2007/05/anatomy-of-query.html
    Premature optimization is the root of all evil in programming. (c) by Donald Knuth
    Naomi Nosonovsky, Sr. Programmer-Analyst
    My blog

  • A method of grouping a total count of errors by date

    Dear SQL Gurus,
    Can anyone guide me (with the script below) as to how I'm can group a total count of error by DISTINCT(CREATION_DATE)?
    11g Enterprise Edition Release 11.2.0.2.0
    SCRIPT:
    select
    CREATION_DATE, request_id, rpad (regexp_substr(summary,'[^\.|^:]+'), 60, '_') as "iCash Dep Error Summary",count(*)
    from BKS_OM.bks_icash_dep_errors
    group by CREATION_DATE, request_id, regexp_substr(summary,'[^\.|^:]+');
    RESULTS:
    CREATION_DATE REQUEST_ID iCash Dep Error Summary COUNT(*)
    02-JUL-12 415967 Validation Failure__________________________________________ 1
    02-JUL-12 415967 Validation Failure__________________________________________ 1
    02-JUL-12 415967 Validation Failure__________________________________________ 1
    02-JUL-12 415967 Validation Failure__________________________________________ 1
    02-JUL-12 415967 Validation Failure__________________________________________ 2
    06-JUL-12 417699 Validation Failure__________________________________________ 1
    06-JUL-12 417699 Validation Failure__________________________________________ 1
    06-JUL-12 417773 Validation Failure__________________________________________ 16
    02-JUL-12 415967 Validation Failure__________________________________________ 1
    06-JUL-12 417773 Validation Failure__________________________________________ 1
    Regards,
    DIBS812
    Edited by: DIBS812 on Aug 3, 2012 10:51 AM

    That did the trick John. Thank you for providing me the solution.
    Thanks everyone for your response!
    select trunc(creation_date), request_id,
    rpad(regexp_substr(summary,'[^\.|^:]+'), 60, '_') as "iCash Dep Error Summary",
    count(*)
    from bks_om.bks_icash_dep_errors
    group by trunc(creation_date), request_id, regexp_substr(summary,'[^\.|^:]+');
    TRUNC(CREATION_DATE) REQUEST_ID iCash Dep Error Summary COUNT(*)
    19-JUL-12 422601 Validation Failure__________________________________________ 326
    19-JUL-12 423343 Validation Failure__________________________________________ 383
    20-JUL-12 423650 Validation Failure__________________________________________ 439
    02-JUL-12 415967 Validation Failure__________________________________________ 53
    16-JUL-12 421687 Validation Failure__________________________________________ 277
    06-JUL-12 417699 Validation Failure__________________________________________ 21
    10-JUL-12 419189 Validation Failure__________________________________________ 124
    06-JUL-12 417773 Validation Failure__________________________________________ 124
    18-JUL-12 422529 Validation Failure__________________________________________ 269
    19-JUL-12 422894 Validation Failure__________________________________________ 326
    19-JUL-12 422901 Validation Failure__________________________________________ 326
    18-JUL-12 422357 Validation Failure__________________________________________ 277
    19-JUL-12 422865 Validation Failure__________________________________________ 326

Maybe you are looking for