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.

Similar Messages

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

  • 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

  • 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

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

  • PL/SQL update rate using having count

    Hi am newbie at pl/sql
    I need to update rate_per_hour for all projects having less than 5 employee to 500
    here is the code what i wrote, but it updates all employee
    set serveroutput on
    declare
      cursor rate_cur is
      select * from project
      for update of rate_per_hour;
    begin
      for rate_rec IN rate_cur
      loop
        update project
        set rate_per_hour=500
        where current of rate_cur;
      end loop;
    end;Here is my tables:
    CREATE TABLE employee(
    empid number(5),
    empname varchar(20),
    address varchar(20),
    no_of_dependents number(5),
    deptno number(5),
    CONSTRAINT EMPLOYEE_PKEY PRIMARY KEY(empid),
    CONSTRAINT EMPLOYEE_FKEY FOREIGN KEY(deptno) REFERENCES department(deptno));
    CREATE TABLE project(
    projectno number(5),
    location varchar(20),
    incharge number(5),
    rate_per_hour number(5),
    CONSTRAINT PROJECT_PKEY PRIMARY KEY(projectno),
    CONSTRAINT PROJECT_FKEY FOREIGN KEY(incharge) REFERENCES employee(empid));
    CREATE TABLE assignment(
    empid number(5),
    projectid number(5),
    hours number(5),
    CONSTRAINT ASSIGNMENT_FKEY FOREIGN KEY(empid) REFERENCES employee(empid),
    CONSTRAINT ASSIGNEMNT_FKEY2 FOREIGN KEY(projectid) REFERENCES project(projectno));please suggest a solution
    Edited by: user12970368 on Dec 15, 2011 6:55 AM

    here is test data
    /*department table */
    insert into department values(201,'ADMINISTRATION',10000,101);
    insert into department values(202,'FINANCE',10000,102);
    insert into department values(203,'ACCOUTING',10000,103);
    insert into department values(204,'HR',10000,104);
    insert into department values(205,'TESTING',10000,105);
    /*employee table
    insert into employee values(101,'Marlen', 'DXB1',5,201);
    insert into employee values(102,'Oleg', 'DXB2',2,201);
    insert into employee values(103,'Adil', 'SHJ1',1,202);
    insert into employee values(104,'Sultan', 'AD1',0,202);
    insert into employee values(105,'John', 'AD2',1,203);
    insert into project values(001,'Dubai Marina',101,50);
    insert into project values(002,'Bur Dubai',101,40);
    insert into project values(003,'Palm Jumeirah',103,60);
    insert into project values(004,'Media city',101,100);
    insert into project values(005,'TECOM',101,30);
    /*assignment table */
    insert into assignment values(101,001,55);
    insert into assignment values(101,004,128);
    insert into assignment values(101,005,77);
    insert into assignment values(102,002,27);
    insert into assignment values(103,003,250);Edited by: user12970368 on Dec 15, 2011 6:54 AM

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

  • How do I use count for this query?

    How do I display all the addresses in a table that have more than one (or >1) account number? I wasn't sure how or if I should use count along with group by and having to get the expected results.

    select address from tablename
    group by address having count(1) > 1;

  • Creation of object in universe to show the count in report

    Hi,
    I am trying to create an object in universe.
    I had a table called projects in which there are projectid and managers and now i want to show in report as below.
    15 PMu2019s assigned to 1-3 Projects
    10 PMu2019s assigned to 4 Projects
    20 PMu2019s assigned to > 5 Projects
    The count of managers assigned to the number of projects. Could any one pls explain in detail how i can do this in universe.
    Thanks,
    Eswar

    Hi,
    Here is the syntax:
    SELECT CAST(SUM(a.nb_Mgr) AS VARCHAR) + ' Managers: 1-2 projects' as Nb_Mgr FROM  
    (SELECT Count(distinct Manager) as nb_Mgr from ISL.dbo.Projects group by Manager having count(Project) <= 2) a
    UNION
    SELECT CAST(SUM(a.nb_Mgr) AS VARCHAR) + ' Managers: 3-5 projects' FROM  
    (SELECT Count(distinct Manager) as nb_Mgr from ISL.dbo.Projects group by Manager having count(Project) > 2 and count(Project) <= 5) a
    UNION
    SELECT CAST(SUM(a.nb_Mgr) AS VARCHAR) + ' Managers: > 5 projects' FROM  
    (SELECT Count(distinct Manager) as nb_Mgr from ISL.dbo.Projects group by Manager having count(Project) > 5) a
    By the way you can create a more sophisticated derived table that let users choosing tha values for the number of projects:
    SELECT CAST(SUM(a.nb_Mgr) AS VARCHAR) + ' Managers < ' + @Prompt('Min threshold','A',,mono,free) +  ' projects' as Nb_Mgr FROM  
    (SELECT Count(distinct Manager) as nb_Mgr from ISL.dbo.Projects group by Manager having count(Project) < @Prompt('Min threshold','N',,mono,free)) a
    UNION
    SELECT CAST(SUM(a.nb_Mgr) AS VARCHAR) + ' Managers between ' + @Prompt('Min threshold','A',,mono,free) + ' and '  + @Prompt('Max threshold','A',,mono,free) +   ' projects' FROM  
    (SELECT Count(distinct Manager) as nb_Mgr from ISL.dbo.Projects group by Manager having count(Project) >= @Prompt('Min threshold','N',,mono,free) and count(Project) <= @Prompt('Max threshold','N',,mono,free)) a
    UNION
    SELECT CAST(SUM(a.nb_Mgr) AS VARCHAR) + ' Managers > '  + @Prompt('Max threshold','A',,mono,free) +  ' projects' FROM  
    (SELECT Count(distinct Manager) as nb_Mgr from ISL.dbo.Projects group by Manager having count(Project) > @Prompt('Max threshold','N',,mono,free)) a
    Regards
    Didier

  • Query Count of rows

    Hi Everyone,
    I have a table Table1
    Table1: ID, Name, Sales, Time
    Sample data:
    1, Loo, Y, 20-JUL-09 01.20.17.000000000 PM
    2, Rey, Y, 20-JUL-09 01.20.17.000000000 PM
    1, Loo, Y, 20-JUL-09 01.20.17.000000000 PM
    1, Loo, Y, 20-JUL-09 01.20.17.000000000 PM
    3, Bon, Y, 20-JUL-09 01.20.17.000000000 PM
    1, Loo, Y, 19-JUL-09 01.20.17.000000000 PM
    I want the result as:
    1, 3
    2, 1
    3, 1
    i.e employee with id 1 and count of his apperaince in the table where data is 20-JULY-09
    Any suggestions and inputs are appreciated.
    Thanks
    Kasi

    Hi,
    user10651875 wrote:
    Thanks that worked.
    If i want to change the query to get the ID which is having a count > 20 keeping all the other clauses, what should i modify?Do you mean you only want to diplay rows where count>20 (if any)?
    Add a HAVING clause, immediately after the GROUP BY clause:
    SELECT    id
    ,       COUNT (*)     AS cnt
    FROM       table1
    WHERE       TRUNC (time)     = DATE '2009-07-20'
    GROUP BY  id
    HAVING       COUNT (*)     > 20;HAVING is similar to WHERE, but WHERE is evaluated before aggregate functions (like COUNT in this example) are compute. HAVING is evaluated after the aggregate functions are computed, therefore, you can use them in the HAVING clause. (In fact, you should never have a HAVING clause that doesn't use aggregate functions.)

  • Having with more than one clause

    Hi
    Is posssible to have a query with more than one clause in having condition
    Example In my query I have Count , Sum and AVG , I need to use 3 conditions in having, Is It possible ?
    Thank you in advance

    Hi,
    yes, in Having you can also use AND and OR.
    with x as (select 1 nr from dual)
    select nr
    from x
    group by nr
    having count(*) = 1
    and sum(nr) = 1Herald ten Dam
    http://htendam.wordpress.com

Maybe you are looking for

  • Unable to stop the event logs on access point console

    Hi team, I have an AIR-LAP1131AG-E-K9 access point having ios c1130-k9w8-mx.124-21a.JHB1. When I am trying to take the console of it there are many logs generated like LWAPP ...Go join the controller, Discover controller etc. and the ap is unable to

  • Background job finished but Error sending data

    Dear all, report is running in my prod server . once this report completed sapuser get mail . when i check the job status it shows Job started Step 001 started (program GP4CF9U420G825I4P7DU8XY49WH, variant CUSTOMEROUTSTD, user ID S2K_BASIS) Spool req

  • What caused the iBook problem?

    I'm writing this for my wife, as she's unable to use her iBook for more than a few minutes before it locks up. I'm serving in the military overseas. My wife and I both use Apple products to chat, email, etc. When I spoke to her yesterday, she was fra

  • InDesign CS6 crash with export to PDF

    When exporting to a PDF file (either print or interactive) in InDesign CS6, my program crashes sporadically, but consistently with one particular page. I have retyped that page several times, changing the graphics and bullets-and it still crashes. An

  • Queries related to Finance - SAP B1.

    Hello everyone, 1) Can we have fund flow statement in SAP B1? Meaning of Fund Flow from Internet: THE FUNDS FLOW STATEMENT SHOWS HOW THE SOURCES OF FUNDS EQUAL THE USES OF FUNDS DURING THE PERIOD COVERED BY THE STATEMENT. 2) Can Amount be displayed i