简体   繁体   English

SQL子查询问题

[英]SQL Sub-Query Troubles

I have a SQL assignment I am working on for school (just a series of query and sub-query questions) and I have them all complete but one. 我有一个正在上学的SQL作业(只是一系列查询和子查询问题),而且我都完成了,但只有一个。 Here is the prompt for reference: 这是供参考的提示:

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

I have the code for the first part, the AM hours. 我有第一部分的代码,上午时间。 It is as such: 就这样:

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

The only thing I cant figure out is how to get only the ones with 200 or more. 我唯一不知道的是如何只获得200个或更多的那些。 It should return only 3 rows but regardless of what I try it still returns all or non. 它应该只返回3行,但是无论我尝试什么,它仍然会返回全部或全部。 I'm pretty sure a sub-query needs to be used here but I cant seem to figure it out. 我很确定这里需要使用子查询,但是我似乎无法弄清楚。 This is what I have tried so far: 到目前为止,这是我尝试过的:

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) ;

Logically I know that is incorrect due to the fact that it will just return the sum of all the packet sizes in that given table. 从逻辑上讲,我知道这是不正确的,因为它只会返回给定表中所有数据包大小的总和。 I cant seem to come up with anyway to make it work without it throwing an error. 我似乎无法想出任何办法使其工作而不会引发错误。 Any help would be appreciated! 任何帮助,将不胜感激!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM