Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2006
    Location
    Columbus, OH
    Posts
    69

    Unanswered: SQL Group by count

    DB2 8.2
    AIX 5L

    I am stumped on figuring out how to write this query


    I have a table with fields employees, depts

    I need to return a list of each dept with the number of employees in each dept. Sure not bad. Only in this table, employees is not unique.

    SELECT dept, count(dept) from table group by dept

    This does not solve the issue with unique employees, so somehow i need to use a distinct clause on the employees but ive gotten nowhere.


    Charlie.

  2. #2
    Join Date
    Nov 2005
    Location
    IL
    Posts
    557
    Charlie,

    You have much bigger issue here hen just writing the SQL.

    Select dept, count(distinct empl) from table group by dept.

    BTW. you might want to pick up a book on sql while at it.
    --
    IBM Certified DBA on DB2 for Linux, UNIX, and Windows

    DB2 v9.7.0.6 os 6.1.0.0

  3. #3
    Join Date
    Sep 2006
    Location
    Columbus, OH
    Posts
    69
    Hmmm, not sure what I drank last night...

    Thanks

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by PKPChuck
    Only in this table, employees is not unique.
    that's not necessarily a bad thing, it just means that an employee can belong to more than one department

    next problem?

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Aug 2012
    Posts
    3
    Quote Originally Posted by Cougar8000 View Post
    BTW. you might want to pick up a book on sql while at it.
    lol - mean :P
    Here's IBM's DB2 online manual. Good stuff
    http://pic.dhe.ibm.com/infocenter/db...10r1/index.jsp
    Last edited by Thomas Wright; 08-28-12 at 13:06.

  6. #6
    Join Date
    Sep 2006
    Location
    Columbus, OH
    Posts
    69
    Quote Originally Posted by Thomas Wright View Post
    lol - mean :P
    Here's IBM's DB2 online manual. Good stuff
    IBM DB2 10.1 Information Center for Linux, UNIX, and Windows
    What causes someone to troll through database forums for a 4 year old post to make a comment like that?

  7. #7
    Join Date
    Jun 2007
    Location
    germany
    Posts
    155
    feel sorry for the guy
    when that is the only thing that he can contribute.
    Dick Brenholtz, Ami in Deutschland

  8. #8
    Join Date
    Aug 2012
    Posts
    3
    Because apparently Google doesn't discriminate against dated material, so why should I? And because this post apparently fit the criteria for "delete records without date field specified date"

    So trolling? No, sorry. Googling? Yes.

    The cause? I've been messing with this old DB2 server all day and found that nifty online manual. So I didn't check the date ... big whoop. Ever seen YouTube?

    Plus - ya never know if that same query is going to lead some poor fool here who doesn't know about IBM's online manual.

    So, what causes someone to so quickly assume trolling and readily reply to a new post on a 4 year old thread after 6 years of only making 68 posts?

  9. #9
    Join Date
    Aug 2012
    Posts
    3
    Quote Originally Posted by dbzTHEdinosaur View Post
    feel sorry for the guy
    when that is the only thing that he can contribute.
    *hint* Check the join date

  10. #10
    Join Date
    Oct 2009
    Location
    221B Baker St.
    Posts
    486
    Hi Thomas,

    Worry not - several of us (i.e. me for one) have been beaten up for "trolling" when it was far from it . . .

    This passes. . .
    Last edited by papadi; 08-28-12 at 23:16.

  11. #11
    Join Date
    Jun 2007
    Location
    germany
    Posts
    155
    Thomas,
    I did notice the join date, the post count (which seems important to you)
    and your sarcasm.

    so, if you can't take the heat, stop moaning.
    Dick Brenholtz, Ami in Deutschland

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •