Results 1 to 6 of 6

Thread: SQL Query

  1. #1
    Join Date
    Jan 2010
    Posts
    72

    Unanswered: SQL Query

    Please help me to write the below SQL query.

    There is a table with group_ids (‘gid’) and emp_ids (‘eid’). Each gid only resides on a single eid and each eid may contain multiple gids. How do you find how many gids are on each and every eid? Please provide the SQL.

    Thanks in advance.
    Last edited by regtha; 05-27-10 at 16:32.

  2. #2
    Join Date
    Jan 2010
    Posts
    18
    You need to use "count" and "group by".

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    This will solve your problem exactly as stated.
    Code:
    SELECT Count([gid])
       FROM dbo.myTable
    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by regtha View Post
    How do you find how many gids are on each and every eid? Please provide the SQL.
    Code:
    SELECT eid
         , COUNT(*) AS gcount
      FROM daTable
    GROUP BY eid
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Dec 2007
    Location
    Richmond, VA
    Posts
    1,328
    Provided Answers: 5
    also, isn't your logic kinda backwards? An Employee can belong to many groups, but a GROUP consists of ONE employee?
    Dave

  6. #6
    Join Date
    May 2010
    Posts
    1
    Thanks for this thread, I also get the same problem.

Posting Permissions

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