| Author |
Topic |
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-03-03 : 17:30:37
|
| I'm just wodnering if you have two select statements, one nested inside another, can you reference tables from the outer loop?for example, say I would like to find all employees who have at least two clients and employees and clients have a one to many relationship.select * from Employee ewhere( select count(*) from Clients c where c.EmployeeId = e.EmployeeId) >= 2This obviously doesn't work - but how would i go about doing something like this? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-03-03 : 17:46:04
|
| Try this:SELECT e.EmployeeId, COUNT(*)FROM Employee eINNER JOIN Clients cON e.EmployeeId = c.EmployeeIdGROUP BY e.EmployeeIdHAVING COUNT(*) >= 2If you need to see more columns, then you can use this as a derived table. Let us know what you'd like to see though so we can help with that.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-03-03 : 18:04:44
|
quote: Originally posted by liptonIcedTea This obviously doesn't work - but how would i go about doing something like this?
Did you try the query? It looks like it'd work just fine to me, although I think Tara's query would be more effecient. |
 |
|
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-03-03 : 18:12:47
|
| Ok... I think I simplified the query too much.What i really want to do is this...There is 3 tables. Employees, Clients and ClientType. (clientType is a reference table on Client)basically, I want to find all the employees who have more Clients with ClientType = 'A' then clientType = 'B'.so i think i'm going to need a nested select statement along the lines of something like this...SELECT e.EmployeeId, COUNT(*)FROM Employee eINNER JOIN Clients cON e.EmployeeId = c.EmployeeIdinner join ClientType ct on ct.ClientTypeId = c.ClientTypeIdGROUP BY e.EmployeeIdHAVING ( select count(*) from ClientType ctwhere ct.ClientType = 'A' ) >= ( select count(*) from ClientType ctwhere ct.ClientType = 'B' ) |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-03-03 : 18:14:16
|
| Please provide a data example for your problem so that we can try it out on our machines. The data examples should show sample rows for all 3 of your tables and the sample data should be representative of your problem.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-03-03 : 18:20:13
|
| Ok... not sure how you want this to be...but say there is an employee tableEmployeeId EmployName1 Tim2 Steveand Client table with 3 columns (ClientType, EmployeeId, ClientId)ClientType EmployeeId Client IdA 1 1A 1 2 A 2 3B 2 4 And a reference table ClientType ClientTypeIdAB What I want to do is write a query which returns all employees that have more ClientType A clients then ClientType B clients.In the data example, Tim will be returned because he has 2 A clients and 0 B Clients whilst Steve has 1 A and 1 B.Am I making any sense? |
 |
|
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-03-03 : 18:50:08
|
| basically, the following will compile and work, but the nested queries need like a WHERE statement where I can add the condition to only get the current employee, but becuase i can't seem to reference anything in the outer loop i can't seem to do that.SELECT e.EmployeeId, COUNT(*)FROM Employee eINNER JOIN Clients cON e.EmployeeId = c.EmployeeIdinner join ClientType ct on ct.ClientTypeId = c.ClientTypeIdGROUP BY e.EmployeeIdHAVING ( select count(*) from ClientType ctwhere ct.ClientType = 'A' ) >= ( select count(*) from ClientType ctwhere ct.ClientType = 'B' ) |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-03-03 : 18:59:30
|
| [code]SELECT E.*FROM @Employee AS EINNER JOIN @Client AS C ON E.EmployeeID = C.EmployeeIDGROUP BY E.EmployeeID, E.EmployeeNameHAVING SUM(CASE WHEN C.ClientType = 'A' THEN 1 ELSE 0 END) > SUM(CASE WHEN C.ClientType = 'B' THEN 1 ELSE 0 END)[/code] |
 |
|
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-03-03 : 19:17:36
|
| HAVING SUM(CASE WHEN C.ClientType = 'A' THEN 1 ELSE 0 END) > SUM(CASE WHEN C.ClientType = 'B' THEN 1 ELSE 0 END)this doesn't seem to work, it just returns the total number of clients, not the ones with A |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-03-03 : 19:22:54
|
quote: Originally posted by liptonIcedTea HAVING SUM(CASE WHEN C.ClientType = 'A' THEN 1 ELSE 0 END) > SUM(CASE WHEN C.ClientType = 'B' THEN 1 ELSE 0 END)this doesn't seem to work, it just returns the total number of clients, not the ones with A
It returns exactly what you specified when you showed us sample data as I was working on a solution too and then saw Lamprey's post. I tried his solution and saw it returned what you wanted (Tim) so I stopped working on the problem. If it isn't working for you, then you need to give us better sample data and show us exactly what it needs to return.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-03-03 : 19:25:45
|
| oh actually sorry it does work.cheersyour a legend! |
 |
|
|
|