SQL SELECT issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gchq
    New Member
    • Jan 2007
    • 96

    SQL SELECT issue

    Hi there

    This seemed the closest to a Sybase forum I can find - and it's probably basic SQL anyway...

    Suppose there is a table and the WHERE clause finds 10 records in the row Employee_ID

    1
    1
    1
    4
    1
    1
    4
    1
    1
    1

    and I want to return the number of employees (in this case 2) how would I do this? It's probably so straightforward I'll kick myself, but at the moment.....
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by gchq
    Hi there

    This seemed the closest to a Sybase forum I can find - and it's probably basic SQL anyway...

    Suppose there is a table and the WHERE clause finds 10 records in the row Employee_ID

    1
    1
    1
    4
    1
    1
    4
    1
    1
    1

    and I want to return the number of employees (in this case 2) how would I do this? It's probably so straightforward I'll kick myself, but at the moment.....

    try:

    select Employee_ID, count(*) from YourTable
    group by Employee_ID

    Comment

    • gchq
      New Member
      • Jan 2007
      • 96

      #3
      Originally posted by ck9663
      try:

      select Employee_ID, count(*) from YourTable
      group by Employee_ID

      The trouble with that is it returns the number of records for each Employee ID! With the example I used that would be:-

      Employee_ID - 1 Count(*) 8
      Employee_ID - 4 Count(*) 2

      Any other ideas?

      Comment

      • ilearneditonline
        Recognized Expert New Member
        • Jul 2007
        • 130

        #4
        Originally posted by ck9663
        try:

        select Employee_ID, count(*) from YourTable
        group by Employee_ID
        try this. It should return a single record of 2

        Code:
        select COUNT(DISTINCT(Employee_ID)) FROM table

        Comment

        • ck9663
          Recognized Expert Specialist
          • Jun 2007
          • 2878

          #5
          Originally posted by ilearneditonlin e
          try this. It should return a single record of 2

          Code:
          select COUNT(DISTINCT(Employee_ID)) FROM table

          or a query within a query


          select count(*) from
          (select Employee_ID, count(*) as cnt from YourTable
          group by Employee_ID) a

          Comment

          • gchq
            New Member
            • Jan 2007
            • 96

            #6
            CK9663 and iLearnItOnline - thank you! Distinct was the key that unlocked the door!

            Comment

            Working...