SQL子查询故障

时间:2017-04-12 06:05:49

标签: sql subquery

我正在为学校做一个SQL任务(只是一系列查询和子查询问题),我把它们都完整但只有一个。以下是提示参考:

10. Find the AM hours with total traffic load with 200 or more. 

我有第一部分的代码,即AM时间。就是这样:

select hour(traffic.ttime), sum(traffic.packetsize)
from traffic
where hour(traffic.ttime) <= 12
group by hour(traffic.ttime);

我唯一不知道的是如何只获得200或更多的那些。它应该只返回3行,但不管我尝试它仍然返回全部或非。我很确定需要在这里使用子查询,但我似乎无法弄明白。这是我到目前为止所尝试的:

select hour(traffic.ttime), sum(traffic.packetsize)
from traffic
where hour(traffic.ttime) <= 12 and (select sum(traffic.packetsize) from 
traffic)>=200
group by hour(traffic.ttime) ;

逻辑上我知道这是不正确的,因为它只会返回给定表中所有数据包大小的总和。我似乎无法想出让它工作而不会抛出错误。任何帮助,将不胜感激!

1 个答案:

答案 0 :(得分:0)

 select hour(traffic.ttime), sum(traffic.packetsize)from traffic where hour(traffic.ttime) <= 12 group by hour(traffic.ttime) having sum(traffic.packetsize)>=200