views:

347

answers:

7
+3  Q: 

SQL: HAVING clause

See the following SQL statement:

SELECT datediff("d", MAX(invoice.date), Now) As Date_Diff
      , MAX(invoice.date) AS max_invoice_date
      , customer.number AS customer_number
FROM invoice 
    INNER JOIN customer 
        ON invoice.customer_number = customer.number
GROUP BY customer.number 

If the the following was added:

HAVING datediff("d", MAX(invoice.date), Now) > 365

would this simply exclude rows with Date_Diff <= 365?

What should be the effect of the HAVING clause here?

EDIT: I am not experiencing what the answers here are saying. A copy of the mdb is at http://hotfile.com/dl/40641614/2353dfc/test.mdb.html (no macros or viruses). VISDATA.EXE is being used to execute the queries.

EDIT2: I think the problem might be VISDATA, because I am experiencing different results via DAO.

+1  A: 

Yes, it would exclude those rows.

RedFilter
Why I am I not experiencing that? Could it be something to do with dates in Access?
Craig Johnston
Not sure, show your data and the output you are getting without the `HAVING` clause.
RedFilter
See my edit above.
Craig Johnston
A: 

Yes, that is what it would do.

Rachel
+4  A: 

As already pointed out, yes, that is the effect. For completeness, 'HAVING' is like 'WHERE', but for the already aggregated (grouped) values (such as, MAX in this case, or SUM, or COUNT, or any of the other aggregate functions).

roe
Just to add a point: where happens before grouping, having happens after grouping has already occurred, and limits the grouped values.
Jeremy
@Jeremy; yes that's what I'm saying (it's working on the already aggregated, i.e. grouped, values).
roe
A: 

WHERE applies to all of the individual rows, so WHERE MAX(...) would match all rows.

HAVING is like WHERE, but within the current group. That means you can do things like HAVING count(*) > 1, which will only show groups with more than one result.

So to answer your question, it would only include rows where the record in the group that has the highest (MAX) date is greater than 365. In this case you are also selecting MAX(date), so yes, it excludes rows with date_diff <= 365.

However, you could select MIN(date) and see the minimum date in all the groups that have a maximum date of greater than 365. In this case it would not exclude "rows" with date_diff <= 365, but rather groups with max(date_diff) <= 365.

Hopefully it's not too confusing...

Nelson
A: 

You may be trying the wrong thing with your MAX. By MAXing the invoice.date column you are effectively looking for the most recent invoice associated with the customer. So effectively the HAVING condition is selecting all those customers who have not had any invoices within the last 365 days.

Is this what you are trying to do? Or are you actually trying to get all customers who have at least one invoice from more than a year ago? If that is the case, then you should put the MAX outside the datediff function.

deemar
I am trying to get customers who have NOT had an invoice for the last 365 days.
Craig Johnston
A: 

That depends on whether you mean rows in the table or rows in the result. The having clause filters the result after grouping, so it would elliminate customers, not invoices.

If you want to filter out the new invoices rather than the customers with new invoices, you should use where instead so that you filter before grouping:

select
  datediff("d",
  max(invoice.date), Now) As Date_Diff,
  max(invoice.date) as max_invoice_date,
  customer.number
from
  invoice 
  inner join customer on invoice.customer_number = customer.number
where
  datediff("d", invoice.date, Now) > 365
group by
  customer.number
Guffa
The aim is to get only customers whose maximum invoice is older than 365 days, which I believe would be achieved with either HAVING or WHERE (because MAX is being used) but I am not experiencing the expected results.
Craig Johnston
@Craig: Then your query should work. If it doesn't, you should look for the error elsewhere. Check for example that the date field is actually a date and not a text field.
Guffa
@Guffa: Yes, I identified VISDATA (query tool) as the cuplrit. It appears to be producing incorrect query results.
Craig Johnston
A: 

I wouldn't use a GROUP BY query at all. Using standard Jet SQL:

  SELECT Customer.Number
  FROM [SELECT DISTINCT Invoice.Customer_Number
     FROM Invoice
     WHERE (((Invoice.[Date])>Date()-365));]. AS Invoices 
  RIGHT JOIN Customer ON Invoices.Customer_Number = Customer.Number
  WHERE (((Invoices.Customer_Number) Is Null));

Using SQL92 compatibility mode:

  SELECT Customer.Number
  FROM (SELECT DISTINCT Invoice.Customer_Number
     FROM Invoice
     WHERE (((Invoice.[Date])>Date()-365));) AS Invoices 
  RIGHT JOIN Customer ON Invoices.Customer_Number = Customer.Number
  WHERE (((Invoices.Customer_Number) Is Null));

The key here is to get a set of the customer numbers who've had an invoice in the last year, and then doing an OUTER JOIN on that result set to return only those not in the set of customers with invoices in the last year.

David-W-Fenton
What is your objection to "GROUP BY"?
Craig Johnston
It's unnecessary. You don't need to find out the Max() or Min() of customers' invoice dates. You just need to find the group of customers who lack invoices during the specified period. Also, GROUP BY on large numbers of records can be very slow, particularly if the field is not indexed.
David-W-Fenton