Reputation: 17
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
Upvotes: 0
Views: 149
Reputation: 52644
Another approach, using window functions to simplify things:
SELECT ident, name, len, wid, qnt - [plan] AS amount
FROM (SELECT *, row_number() OVER (PARTITION BY len, wid ORDER BY qnt - [plan] DESC) AS rn
FROM test WHERE name = 'Name1') AS sq
WHERE rn = 1
ORDER BY amount DESC;
Upvotes: 1
Reputation: 6038
It's not clear what kind of database you're using, but this solution should work on any DB:
SELECT tab.Ident,
tab.Name,
tab.Len,
tab.Wid,
(tab.Qty - tab.Plan) AS Amount
FROM (SELECT Name,
Len,
Wid,
MAX(Qty-Plan) AS Amount
FROM my_table
GROUP BY Name,
Len,
Wid
) AS grouped
JOIN my_table tab
ON grouped.Name = tab.Name
AND grouped.Len = tab.Len
AND grouped.Wid = tab.Wid
AND grouped.Amount = (tab.Qty - tab.Plan)
AND tab.Name = 'Name1'
Upvotes: 1