开发者

SELECT id_field WHERE max(date_field) < 'some date'

I'm sure this question is obvious, but I've been fiddling with an increasingly complex set of subqueries for the last hour or so and getting nowhere.

I need to select a bunch of ID's from a ta开发者_如何学JAVAble where the last date for that record is before a given date. I was trying:

SELECT id_field WHERE max(date_field) < 'some date'

But getting 'can't have aggregate in where field'. I've considered that I could SELECT where there are no dates above a certain date, but at this point my brain is waving it's little white flag.

Thanks.


SELECT id_field
     , max(date_field)
  FROM tbl
GROUP BY id_field
HAVING max(date_field) < 'some date'


SELECT id_field 
  FROM tbl 
 GROUP BY id_field 
HAVING max(date_field) < 'some date' 


Use HAVING instead of WHERE. HAVING is like a where for grouped values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜