我尝试获取一个维度按另一个维度分组的百分比。所以说我有
table1 =
country, product, sale
US, TV, 50
US, TV, 150
MEX, TV, 50
US, PC, 800我想在国家/地区按产品分组,并获得:
Select... =
Country, Product, Country_Total, Product_Per_Country, Percentage
US, TV, 1000, 200, 20%
US, PC, 1000, 800, 80%
MEX, TV, 50, 50, 100%我只想从table1中选择一次,因为这是一个扩展的select。你能帮上忙吗?
致以问候,彼得
发布于 2019-07-25 18:58:47
使用窗口函数:
select country, product, sum(sale) as sales,
sum(sale) / sum(sum(sale)) over (partition by country) as country_ratio
from table1 t1
group by country, product;https://stackoverflow.com/questions/57200349
复制相似问题