I have the following query:
select top 1 fact.vend, fact.cliente, sum((detalle.precio-art.costo)*cant) as total
from afacturas as fact
inner join afacart as detalle on fact.comprob = detalle.comprob
inner join aarticulos as art on detalle.cordart = art.codigo
group by fact.cliente,fact.vend
order by vend
The one that returns the accumulated total for each client that each vendor has:
1 1 1800
1 12 4800
2 5 2300
2 11 42500
3 2 4600
3 4 1700000
4 16 160000
What I want to do is to return only the top 1 of each customer that has the highest amount of money per seller.
For example:
1 12 4800
2 11 42500
3 4 1700000
4 16 160000