获取列分组计数的简单查询

时间:2016-12-20 10:00:17

标签: sql

我正在做一些例子查询

我需要以白色获得以下输出(在提交sql下面)

以下是我的查询,绿色是查询的输出,这是file pickers

 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 

relation of tables

3 个答案:

答案 0 :(得分:2)

尝试使用如果您在group by中添加guest_id,那么只需nights group by,您的代码就可以了。 COUNT&通过使用以下两列对数据进行分组,将相应地计算SUM

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

答案 1 :(得分:0)

尝试以下查询

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

答案 2 :(得分:-1)

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