function - SQL: How to return MAX and MIN in one row? -
i want last cost latest costing date , minimum cost products.
when use query below, giving me max date , min cost each column. please see screenshots below.
select max(costingdate) latestdate, min(cost) minprice, outletcode, productid accountscosting outletcode = 'c&t01' group outletcode, productid
result:
e.g - productid: 200006
select * accountscosting productid = 200006 , outletcode = 'c&t01' order costingdate desc
what want last costing date minimum cost (the 1 highlighted red color). if purchase date same 2013-03-20
, should return minimum cost.
how can edit query result? appreciated!
first need latest date can find minimum cost them. e.g.
select a.outletcode, a.productid, latestdate, min(cost) minprice ( select max(costingdate) latestdate, outletcode, productid accountscosting outletcode = 'c&t01' group outletcode, productid ) left join accountscosting b on a.outletcode=b.outletcode , a.productid=b.productid , a.latestdate=b.costingdate group a.outletcode, a.productid, latestdate
Comments
Post a Comment