Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
JBee
Starting Member
9 Posts |
Posted - 2007-12-28 : 10:33:32
|
| Hi, hope someone can help.I have a table of employees and two sites. Most employees only do shifts at the one site but may occasionally work at the other site. What i want is the details of those who have worked at both sites.for e.g.Employees1 Dave2 Peter3 JohnSite 1Mon JohnTue PeterWed JohnThu JohnFri PeterSite 2Mon DaveTue DaveWed DaveThu DaveFri PeterSo the answer should be 2 Peter. Hope this explains the problem. Probably very obvious but it has me stumped.Thanks |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2007-12-28 : 10:56:40
|
what have you tried so far Be One with the OptimizerTG |
 |
|
|
JBee
Starting Member
9 Posts |
Posted - 2007-12-28 : 11:25:05
|
| Well so far i have this:SELECT staffid, name, COUNT(Site1.Staffid) as totalFROM staff INNER JOIN Site1 ON staff.staffid = site1.staffidGROUP BY staffid, nameWhich gets me a count of how many shifts each member of staff have done at one site (site1).this seems a far cry from what i want! |
 |
|
|
JBee
Starting Member
9 Posts |
Posted - 2007-12-28 : 11:43:51
|
| I think i have solved it below - but can't try just yet. Anyway hopefully that will do it first thing am.select staff.staffid, staff.namefrom staff inner join site1 on staff.staffid = site1.staffidinner join site2 on staff.staffid = site2.staffidwhere site1.staffid = site2.staffid |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2007-12-28 : 11:53:10
|
you just about have it if you combine your two solutions:select staff.staffid ,staff.namefrom staff inner join site1 on staff.staffid = site1.staffidinner join site2 on staff.staffid = site2.staffidgroup by staff.staffid ,staff.name Be One with the OptimizerTG |
 |
|
|
|
|
|