Moudiz
Moudiz

Reputation: 7387

simple query that gets the count of column grouping by them

I am doing some examples queries

I need to get the below output in white (below submit sql)

the below is my query and in the green the output of query and this is the relation of tables

 select guest_id, count(nights) , sum(nights) from booking 
inner join guest on guest.id=booking.guest_id
where guest_id in(1185,1270)
group by guest_id,nights 

enter image description here

Upvotes: 0

Views: 58

Answers (3)

Yukti Gupta
Yukti Gupta

Reputation: 1

SELECT guest_id, 
       Count(nights), 
       Sum(nights) 
FROM   booking 
WHERE  guest_id IN ( 1185, 1270 ) 
GROUP  BY guest_id

Upvotes: -1

purva
purva

Reputation: 1

try following query

Select a.guest_id,count(nights),sum(nights) from booking a inner join guest b on a.guest_id=b.id where a.guest_id in (1185,1270) group by a.guest_id

Upvotes: 0

Shushil Bohara
Shushil Bohara

Reputation: 5656

TRY THIS Everything is fine with your code just you have to group by only guest_id, if you add nights also in group by then COUNT & SUM will be calculated accordingly by grouping data using both the columns:

select guest_id, count(nights) , sum(nights) 
from booking 
inner join guest on guest.id=booking.guest_id
where guest_id in(1185,1270)
group by guest_id

Upvotes: 3

Related Questions