I'm trying to select the highest value(calculated) of a group with distinct selection of other columns
according to the table-data below, i want to select the rows, which have the highest amount (Qty-Plan) and a distinct selection of Len and Wid
Table data is as follow
+-----------+-----------+---------+---------+------------+---------+ | Ident | Name | Len | Wid | Qty | Plan | +-----------+-----------+---------+---------+------------+---------+ | 12345 | Name1 | 1500 | 1000 | 20 | 5 | | 23456 | Name1 | 1500 | 1000 | 30 | 13 | | 34567 | Name1 | 2500 | 1000 | 10 | 2 | | 45678 | Name1 | 2500 | 1000 | 10 | 4 | | 56789 | Name1 | 1500 | 1200 | 20 | 3 | | 00001 | Name2 | 1500 | 1200 | 10 | 6 | | 00002 | Name2 | 1500 | 1200 | 20 | 7 | | 00003 | Name3 | 1500 | 1200 | 30 | 5 | | 00004 | Name3 | 1500 | 1200 | 40 | 4 | +-----------+-----------+---------+---------+------------+---------+ with my query i cant erase the "lower" values:
select a.Ident ,a.Name, a.Len,a.Wid, a.Qnt-a.Plan as Amount from table a join (select ident, max(Qnt - Plan) Amount from table where Name = 'Name1' group by Ident, Len, Wid) b on b.Ident = a.Ident and b.Amount = a.Qnt-a.Plan order by Amount desc off-topic question: why i cant use -> where b.Amount = a.Amount (he does not know a.Amount ) ???
my desired select should look like:
+-----------+-----------+---------+---------+------------+ | Ident | Name | Len | Wid | Amount | +-----------+-----------+---------+---------+------------+ | 56789 | Name1 | 1500 | 1200 | 18 | | 23456 | Name1 | 1500 | 1000 | 17 | | 34567 | Name1 | 2500 | 1000 | 8 | +-----------+-----------+---------+---------+------------+ thanks a lot in advance
the rows, which have the highest amount (Qty-Plan), but in thedesired selectI see amounts17, 8, 18. What is the highest?