dbTalk Databases Forums  

SQL Help

comp.databases comp.databases


Discuss SQL Help in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
kramer
 
Posts: n/a

Default SQL Help - 08-13-2007 , 03:41 PM






To I have the following tables:

groupleaders, workgroups, employees

employees,
first name
last name
employeeid

workgroups
groupname
groupid

groupmembership
employeeid
groupid

Thus, a record (3, 4) in the groupmembership table means that the
employee with id 3 is a member of the group with id 4.

Note that employees can be members of more than one group.

So my question is this: is there an easy way with SQL to do a query
that determines the employee who "shares" a group with (i.e. is in a
group with) the most other employees?

Thanks.


Reply With Quote
  #2  
Old   
Tonkuma
 
Posts: n/a

Default Re: SQL Help - 08-19-2007 , 01:39 PM






I'm not so well in English. So, I may be some misunderstandig of your
requirement.
(It will be better to show sample data and expected result by you.)

This may be an answer, if I understand exactly your requirements.

WITH shared(employeeid, groupid, number_of_shared) AS (
SELECT E.employeeid, M.groupid, COUNT(M.employeeid)
FROM employees E
INNER JOIN
groupmembership M
ON E.employeeid = M.employeeid
AND E.employeeid <> M.employeeid
GROUP BY E.employeeid, M.groupid
)
SELECT S.*
FROM shared S
WHERE (employeeid, groupid, number_of_shared) IN
(SELECT employeeid, groupid, number_of_shared
FROM shared
WHERE number_of_shared =
(SELECT MAX(number_of_shared)
FROM shared
)
)
;


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.