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 |
zeezrom011
Starting Member
2 Posts |
Posted - 2007-02-28 : 12:28:14
|
I have two queries that I want to join but when I do the column that holds a count goes nuts. I've tried joins and subqueries but I can't figure it out. Queries in question are:select r.respemp, r.location, r.errtype, r.errdesc,case when r.respemp is null then 'UNKNOWN' ELSE r.empname end as EmpName, count(r.arpnum) as ErrCountfrom RegistrationInfo r where r.admitdate between '10/31/2006' and '12/31/2006'group by r.respemp, r.location, r.errtype, r.errdesc, empnameselect respemp, count(arpnum) from RegsByEmp where admitdate between '10/31/2006' and '12/31/2006' group by respemporder by respempAny and all help is appreciated. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-02-28 : 14:09:17
|
[code]SELECT p.respemp, p.location, p.errtype, p.errdesc, p.errcountFROM ( select r.respemp, r.location, r.errtype, r.errdesc, case when r.respemp is null then 'UNKNOWN' ELSE r.empname end as EmpName, count(r.arpnum) as ErrCount from RegistrationInfo as r where r.admitdate between '10/31/2006' and '12/31/2006' group by r.respemp, r.location, r.errtype, r.errdesc, case when r.respemp is null then 'UNKNOWN' ELSE r.empname end ) AS pINNER JOIN ( select respemp, count(arpnum) from RegsByEmp where admitdate between '10/31/2006' and '12/31/2006' group by respemp ) AS q ON q.respemp = p.respempORDER BY p.respemp, p.location[/code]Peter LarssonHelsingborg, Sweden |
 |
|
zeezrom011
Starting Member
2 Posts |
Posted - 2007-02-28 : 14:38:05
|
Thank you Peter, that solution is excellent |
 |
|
|
|
|
|
|