【问题标题】:How do I get the inner join and sub-sql correct?如何获得正确的内部联接和子 sql?
【发布时间】:2020-02-08 21:01:44
【问题描述】:

问题来了:

哪九个县的人均(人均)酒瓶销量最高?计算这些县销售的酒瓶的平均瓶数,四舍五入到最接近的小数点后一位。人均酒瓶销量最高的九个县中,哪个县的(平均)酒瓶尺寸最大?

以下是表格:

这是我到目前为止的代码:

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 很陌生。请帮我解决一下这个。 非常感谢!!!

【问题讨论】:

  • 请用您正在使用的数据库标记您的问题。

标签: sql join group-by


【解决方案1】:

我认为你想要加入和聚合:

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

查询连接表countiessalesproducts,然后通过county聚合。人均瓶数的计算方法是将销售额之和bottle_quantity除以该县的人口;我们使用它来对结果进行排序以限制结果的数量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 2018-06-17
    • 2013-07-28
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多