开发者

Selecting the most recent entry subject to a condition

I have a table with a 'date' field (written in an integer form), and a 'grocery_item' field. The date specifies when a certain grocery item was ordered

I am trying to write a query that slelect the most recent entry for every grocery items that occured before a given date:

ex:

id  | date    |  grocery_item
1开发者_如何学编程   | 201101  |  a
2   | 201101  |  b
3   | 201102  |  a
4   | 201103  |  b
5   | 201104  |  c

get most recent that occured before 201103

id  | date    |  grocery_item
2   | 201101  |  b
3   | 201102  |  a

Any help will be more than appreciated!! -- I am blanking out on this ...


SELECT id, MAX(date) AS date, grocery_item
FROM table
WHERE date < 201103
GROUP BY grocery_item
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜