You cannot use aggregate functions after WHERE
clause because it makes no sense, whereas DATEDIFF
is usable. It makes no sense because it would be like telling the computer:
select all the rows for which the maximum of the id column is 34
which may sound somewhat logical but is not really, and is in fact the same as id < 35
. that second way is also way clearer.
The real problem is in fact that counting the max of a column in a selection requires that a selection already be made. Hence MAX
can't be used as a condition to actually select rows to be evaluated.
as for DATEDIFF
, it works because the resulting question, for the computer, is logical:
select all the rows for which the difference between date1 and date2 is less then 10