开发者

Get maximum value for each item in a table

Alright, that title was terrible.

Specifically, I have a table of barcodes that were scanned (many times, the table keeps a journal of all scans) and has a created_on column when the scan was created.

I want to get the most recent scan for each barcode. There is other data, but I have trimmed it out and simplified stuff below.

If my table is like this:

BARCODE  CREATED_ON
   1       5/7/11
   2       5/6/11
   1 开发者_开发百科      5/5/11
   1       5/5/11
   2       5/8/11
   3       5/10/11
   3       5/2/11

I want the query to give me this:

BARCODE  CREATED_ON
   1       5/7/11
   2       5/8/11
   3       5/10/11

So, basically for each param1 (barcode in this example), I want the entry with the max param2 (created_on).

Best way to do this?


SELECT BARCODE,MAX(CREATED_ON)
FROM barcodes
GROUP BY BARCODE

Optionally you could add this to the end to sort by barcode:

ORDER BY BARCODE ASC


 select barcode, max(created_on) from barcodes group by barcode
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜