下面是问题:
哪九个县的人均白酒销售量最高?计算这些县出售的白酒的平均瓶子大小,四舍五入到最接近的第一位小数点。在人均白酒销量最高的9个县中,哪个县的酒瓶(平均)最大?
下面是几个表:

以下是我目前掌握的代码:
sql = """
Select s.county, (s.bottle_qty/c.population) As sale_per_cap, lar_bott_sz = (Select p.bottle_size
From products As p
Where s.item = p.item_no)
From sales As s, counties As c
Inner Join counties On c.county = s.county
Group By c.county
Order By sale_per_cap
Limit 10
"""我是SQL的新手。请帮我弄一下这个。非常感谢!
发布于 2020-02-09 05:11:19
我认为您需要连接和聚合:
select
c.county,
sum(s.bottle_quantity) / c.population bottles_per_person,
avg(p.bottle_size) avg_bottle_size
from counties c
inner join sales s on s.county = c.county
inner join products p on p.item_no = s.item
group by c.county, c.population
order by bottles_per_person desc
limit 9该查询连接表counties、sales和products,然后按county聚合。每个人的瓶子比率是通过将销售bottle_quantity的总和除以该县的人口来计算的;我们使用它来对结果进行排序并限制结果的数量。
https://stackoverflow.com/questions/60131148
复制相似问题