简体   繁体   中英

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. 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. It should return only 3 rows but regardless of what I try it still returns all or non. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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