日期 | 編號 | 倉庫 | 數量 |
2012-05-31 | C001 | A店 | 136 |
2012-05-29 | C001 | A店 | 139 |
2012-05-29 | C001 | B店 | 5 |
2012-05-30 | C001 | B店 | 6 |
我只顯示最大日期的記錄,這個SQL怎么寫呀?
即
日期 | 編號 | 倉庫 | 數量 |
2012-05-31 | C001 | A店 | 136 |
2012-05-30 | C001 | B店 | 6 |
SQL code:
方法一:
select * from tb t where not exists(select 1 from tb where `編號`=t.`編號` and `倉庫`=t.`倉庫` and `日期`>t.`日期`)
方法二:
SELECT * FROM tb T, (SELECT `編號`,MAX(`日期`) rq FROM tb GROUP BY `編號`) t2 where t.`編號`=t2.`編號` and t.`日期`>=t2.rq
方法三:
select * from tb a where a.`日期` in (select max(b.`日期`) from table b where b.id=a.id)