Mysql 查询 - 按国家、地区的总计和占总计的百分比(一般)

Mysql query - summ total by country, region and take percentage of total (general)

我有两个table 1.地理 2. 销售额

  1. 带字段的地理位置 国家、城市、userId

  2. 带字段的销售额 UserId, sales_amount

我需要查询并获得结果 国家/城市/sales_amount(按城市)/占总(一般)百分比(按城市)

如何在不加入销售 table 的情况下成功? 你能帮我查询一下吗?

我只知道自己联合销售(但行不通)

我试过将 Sales 连接到自身,但它不起作用

Select Geo.country, Geo.city, sum(Sales_amount), 
(sum(Sales_amount))/"t" as "percentage of general total"
From Geo
Join 
Sales
On Geo.UserId = Sales.UserId
Join
(Select userId, summ(sales_amount) as "total" from Sales) as "t" 
ON t.userId = Geo.UserId
Group by Geo.country, Geo.city

没有任何结果(

表之间的连接,然后按城市求和。
可以通过将每个总和除以总数来检索百分比:

select 
  g.country, 
  g.city,
  sum(s.sales_amount) citysales,
  100.0 * sum(s.sales_amount) / (select sum(sales_amount) from sales) percentage
from geo g left join sales s
on g.userid = s.userid
group by g.country, g.city