开发者

Selecting the most recent entry subject to a condition

开发者 https://www.devze.com 2023-03-22 13:35 出处:网络
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 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

精彩评论

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

关注公众号