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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 returning count when count is also part of filter

Author  Topic 

jonjsilver2
Starting Member

14 Posts

Posted - 2011-08-30 : 10:30:23
I'm just being braindead and maybe someone can help.

I have the following tables:

Employee
Empid EmpName EmpDoB

employeeDepend
empid DepName DepDoB

DepartmentEmployees
DeptId EmpId

Department
DeptId DeptName

I would like the following info returned
EmpId, EmpName, DeptName, NumberofDependents,
Where the number of dependents for the employee > 2

Can someone help me with the query?
thanks!
jon

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-30 : 10:46:20
[code]SELECT e.Empid,
e.EmpName,
ed.DepName
FROM Employee e
INNER JOIN employeeDepend ed
ON ed.empid = e.empid
INNER JOIN (SELECT empid,COUNT(DepName) AS DeptCnt
FROM employeeDepend
GROUP BY empid)ed1
ON ed1.empid = ed.empid
WHERE ed1.DeptCnt > 2
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -