我有一個表名batch_log其結構獲取最大記錄是如下查詢從表中
batch_id run_count start_date end_date
1 4 03/12/2014 03/12/2014
1 3 02/12/2014 02/12/2014
1 2 01/12/2014 01/12/2014
1 1 30/11/2014 30/11/2014
2 5 03/12/2014 03/12/2014
2 4 02/12/2014 02/12/2014
2 3 01/12/2014 01/12/2014
2 2 30/11/2014 30/11/2014
2 1 29/11/2014 29/11/2014
3 3 02/12/2014 02/12/2014
3 2 01/12/2014 01/12/2014
3 1 30/11/2014 30/11/2014
我需要爲所有最大run_count的BATCH_ID提取行。查詢 結果應該是:
batch_id run_count start_date end_date
1 4 03/12/2014 03/12/2014
2 5 03/12/2014 03/12/2014
3 3 02/12/2014 02/12/2014
我試着用很多選項,通過BATCH_ID和run_count組,但沒能得到正確的結果
select a.* from batch_log a,batch_log b
where a.batch_id =b.batch_id
and a.run_count=b.run_count
and a.run_count in (select max(run_count) from batch_log
group by batch_id) order by a.batch_id
普萊舍幫助
http://stackoverflow.com/questions/27185746/how-to-get-latest-two-rows-with-certain-value-by -date-in-sql或http://stackoverflow.com/questions/27011494/group-by-with-maxtimestamp或http://stackoverflow.com/questions/3491329/group-by-with-maxdate – 2014-12-03 08:36:45