-2
基本上我有几个列的表格,但我只在4我的表有兴趣的是这样的:SQL分度号由不同的小组由结果
accountID productID profit cost date country city quantity
2 10 25 15 2017/03/13 Afghanistan Kabul 330
18 3 45 42 2017/05/14 UK London 5300
25 14 22 17 2017/05/21 UK London 300
3 11 30 26 2017/04/23 Afghanistan Herat 400
我想实现的是让产品总量按城市,按国家和city_quantity/country_quantity的比例:
country city city_quantity country_quantity city_percentage
Afghanistan Kabul 800 1400 0.57
Afghanistan Kandahar 400 1400 0.29
Afghanistan Herat 200 1400 0.14
UK London 6500 10000 0.65
UK Manchester 3000 10000 0.3
UK Newcastle 500 10000 0.05
到目前为止,我的剧本是这样的:
select country, city, sum(quantity)
from table
where date > dateadd(month,-1,getdate())
group by country, city
order by country, city
Where条件是因为我只想要上个月的数据,所以认为它是不相关的。
如何用简单的脚本实现我想要的功能?
我想你会向我们展示原始表格中的数据。 –
我可以看到数据集和所需结果之间没有关联。请参阅:[为什么我应该为我认为是非常简单的SQL查询提供一个MCVE?](https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-对于什么似乎对我来说是一个非常简单的SQL查询) – Strawberry
我显然没有只有我提供的原始表上的行。这也是我的第一个关于Stack Overflow的问题,请耐心等待。我刚刚创建了一些行,以便更好地描述我想要的内容,因为我必须处理的数据是非常机密的。但无论如何,根据我的“原始表”,我怎样才能得到每个城市的总量,每个国家的总量以及这两者之间的比率?根据我的数据说,我应该有3行:英国伦敦5700 5700 1,阿富汗喀布尔330 730 0.45和阿富汗赫拉特400 730 0.55 – whateveros