SQL Query problems

3

I'm having problems with the following query:

SELECT Cliente.Nome, (Reserva.NumeroNoites) TotalNoites 
From Reserva 
inner join Cliente on Reserva.ID_Cliente = Cliente.ID_Cliente 
group by Reserva.NumeroNoites, Cliente.Nome

I would like to select clients with the highest number of nights ... However when doing my query ... the result is

Cliente Barbosa : 1 noite;
Cliente Silva: 1 noite;
Cliente Silva: 2 noites;

Here is the problem ... in the graphic appears 2 times the same client (Silva) in which the correct would appear only once with the total of 3 Nights. Can someone help me?

    
asked by anonymous 28.05.2018 / 11:39

1 answer

3

The problem is that you are not adding up the total of nights; what you are doing is just listing the nights and the customers (so if the customer has 10 reservations, it will be displayed 10 times). Try the query below:

SELECT Cliente.Nome, SUM(Reserva.NumeroNoites) as TotalNoites 
From Reserva 
inner join Cliente on Reserva.ID_Cliente = Cliente.ID_Cliente 
group by Cliente.Nome
    
28.05.2018 / 12:45