I have a 3 columns
nid aid cst
1 1 23
2 3 45
3 1 67
4 2 34
5 3 12
6 1 6
Please re-read for explanation: I have to find the cst of aid which has minimum value of nid for example whne i select aid=1 then it must give 23 as it correponds to mimimum value of nid(1) and when i select 3 it must give 45 as it has least nid(2) as compared to all other nids of aid
My try is this ad it do not work :
select cst from tbl
where (nid) IN (select min(nid) from tbl) and aid=nid
I also have to do the same for max as i done for min.
One method uses row_number()
:
select t.*
from (select t.*, row_number() over (partition by aid order by cst) as seqnum
from tbl t
) t
where seqnum = 1;
Your method would work with table aliases, qualified column names, and the correct logic:
select t.cst
from tbl t
where t.cst = (select min(t2.cst) from tbl t2 where t2.aid = t.aid);
Or, more simply:
select aid, min(cst)
from t
group by aid;
select cst
from tbl
where aid=1 and nid=(select min(nid) from tbl where aid=1)
This query will give you minimum values of cst
for all values of aid
select cst from tbl_NID where aid =1 and nid in ( select min (nid) from tbl_NID where aid=1 )
select cst from tbl_NID where aid =1 and nid like ( select min (nid) from tbl_NID where aid=1 )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.