I need to get the top 2 clients that spend more money but without repeating.
I have this code:
Select DISTINCT Top 2 Clientes.Nome, Clientes.NCartao, Vendas.ValorCIva
from Clientes AS Cli, Vendas
INNER JOIN Clientes ON Clientes.IdCliente = Vendas.IdCliente
order by Vendas.ValorCIva DESC
and I have this result:
Client Number Total
José Pinto 123456 8,48
José Pinto 123456 6,52
Joao Ferreira 564789 5,75
but i want this:
Client Number Total
José Pinto 123456 15
Joao Ferreira 564789 5,75
One way of doing it:
select Top 2 Clientes.Nome, Clientes.NCartao, sum(Vendas.ValorCIva) ValorCIva
from Clientes AS Cli, Vendas INNER JOIN Clientes ON Clientes.IdCliente = Vendas.IdCliente
group by Clientes.Nome, Clientes.NCartao
order by sum(Vendas.ValorCIva) DESC
if you dont want to repeat that client put a GROUP BY so it will automatically display each client and dont repeat itself, something like this..
Select Top 2 Clientes.Nome, Clientes.NCartao, Vendas.ValorCIva
FROM Clientes AS Cli, Vendas
INNER JOIN Clientes ON Clientes.IdCliente = Vendas.IdCliente
GROUP BY Clientes.Nome
ORDER BY Clientes.Nome
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.