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:

enter image description here

e.g - productid: 200006

select * accountscosting productid = 200006 , outletcode = 'c&t01' order costingdate desc 

enter image description here

what want last costing date minimum cost (the 1 highlighted red color). if purchase date same 2013-03-20, should return minimum cost.

enter image description here

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

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -