Jerome wrote:
Hi, sorry for my messed up coding ...
Here's the SQL code (accessing an Access DB):
SELECT Count(museebus_Site.IDUmeldung) AS NP
FROM museebus_Site GROUP BY museebus_Site.printed HAVING
(((museebus_Site.printed)=No));
Evertian has it correct. You are confused about when it is correct to use
Having instead of Where. The simple explanation is: WHERE conditions are
applied BEFORE the records are grouped and aggregations are calculated.
HAVING conditions are applied AFTER the grouping. Whenever possible, you
should use WHERE, because the fewer records you supply to the grouping
mechanism, the better your query will perform. Only use HAVING conditions
when you need to evaluate the records after the grouping and aggregation are
completed.
Some guidelines:
1. If the field you wish to filter appears in the GROUP BY clause, use WHERE
Select a, sum(b)
from table
where a = 3
group by a
This query will return a single row
2. If you wish to limit the resultset based on the result of an aggregation,
use HAVING
Select a, sum(b)
from table
group by a
HAVING sum(b) > 25
This query will return only rows where the sum is greater than 25.
3. If the field you are grouping by does not appear in the SELECT list, then
there is no need to put it in the GROUP BY clause:
Select sum(b)
from table
where a = 3
This query will again return a single row
4. If the field you wish to filter appears neither in the GROUP BY list nor
the SELECT list, use WHERE:
Select a, sum(b)
from table
where c = 3
group by a
HAVING sum(b) > 25
The reason that your query returned no records is because the HAVING clause
was applied to the records resulting from the grouping operation. These
records contained no field called printed, so the HAVING condition could
never be satisfied, resulting in no records being returned.
HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"