There are a couple of problems with your query.
First of all, you are doing a cross join between tblMember and tblIssueReturn. Say you have 100 "issue return" records (whatever those are...) and 50 of them match your criteria (between May 1 and June 1 of 2010, and the expected return date is equal to the return date). Then, say you have 10 members. In this case, you will get 500 matches. You probably don't mean to do this--perhaps you just forgot the relationship between tblMember and tblIssueReturn in your query?--but this will result in your query doing something you don't expect it to do.
This leads me to note #2, which is kind of a problem: you're using a deprecated method for joins. It is highly recommended that you explicitly name your joins. Instead of
from
tblmember,
tblIssueReturn
where
tblmember.something = tblIssueReturn.something
You would have:
from
tblmember
inner join tblIssueReturn on tblmember.something = tblIssueReturn.something
This prevents you from creating accidental cross joins.
Getting a little more niggling, I would recommend using the ISO format for dates in SQL Server: instead of '2010/5/1', it's better to use '2010-05-01' because there is no ambiguity. If you run '2010/5/1' on a database with, say, a French collation, it will not work the way you think it should.
Getting to the main question that you have, @TRAD is right: you need a HAVING clause (and his code just had an incorrect GROUP BY). Think of HAVING versus WHERE like this: you use WHERE when you want to filter out **elements**, but you use HAVING when you want to filter out **groups which have already been aggregated**.
Your currently broken query (because it is doing a cross join) would then look like:
select
COUNT(*) as mycount,
tblmember.memberName
from
tblmember,
tblIssueReturn
where
tblIssueReturn.ExpectedReturnDt = tblIssueReturn.ReturnDate
and tblIssueReturn.ReturnDate between '2010/5/1' and '2011/6/1'
group by memberName
having
COUNT(*) > 3;
But again, it is a broken query: you'll need to fix the cross join before it returns the correct values.
↧