[英]SQL query: for each state with specific number of sailors
I'm starting to learn to write SQL queries. 我开始学习编写SQL查询。 However, I'm still struggling with that.
但是,我仍在为此而苦苦挣扎。 I had to write a query that gives back for each state that has more than 5 sailors the sailor id and the total number of reservations that he has made.
我必须编写一个查询,该查询针对拥有5个以上水手的每个州返回水手ID和他已进行的预订总数。 The schemas are as following:
模式如下:
Slr
( sid
, sname
, rating
, state
) and Reserves
( sid
, bid
, day
). Slr
( sid
, sname
, rating
, state
)和Reserves
( sid
, bid
, day
)。
Here's my trial: 这是我的审判:
Select slr.state, slr.sid, count(*)
From slr left join Reserves on slr.sid=reserves.sid
Group By s.state
Having count(*) >= 5
I know it's not correct, but what can I change ? 我知道这是不正确的,但是我可以更改什么?
Something like this should get you started. 这样的事情应该会让您入门。
select slr.id
, state
, count(*) records
from slr join
(select state thestate, count(*) staterecords
from slr
group by state
having count(*) >= 5 ) temp on state = thestate
group by slr.id
This gives you the sailor id's in states with 5 or more sailors. 这使您的州的水手ID达到5个或更多。 There is nothing in your question that helps me with the part about how many reservations he has made.
您的问题中没有什么对他保留了多少保留项有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.