我有所有国家/地区的平均行,我尝试计算所有平均值的平均值并添加特定输入的行。
我能够通过聚合和平均动作来计算所有国家的平均值,但我不知道如何添加特定的输入线。
输入:
输出:
| Country | Average |
| All | 1.72096666666 |
| GB | 1.64992311635 |
也许是这样的? (如果我错过了你的观点,请告诉我):
(YOUR_AGGEGATE_QUERY_GOES_HERE)
UNION
(SELECT CountryRegionCode as Country, AverageOrdersPerCostumer as Average FROM table_name
WHERE CountryRegionCode = "GB")
如果你想使用行id,你可以调整WHERE语句,但你明白了。
如果您使用的是oracle数据库,那么您可以尝试使用汇总聚合函数,它将完全符合您的要求...
以下是供您参考的链接......
https://docs.oracle.com/cd/B28359_01/server.111/b28313/aggreg.htm#i1007413
https://oracle-base.com/articles/misc/rollup-cube-grouping-functions-and-grouping-sets
这肯定会奏效:
select 'ALL',avg(averageorderpercustomer) from tablename group by
countryregioncode union
select countryregioncode,averageorderpercustomer from tablename where
countryregioncode=5;
你似乎想要:
SELECT 'All' as Country, AVG(AverageOrdersPerCostumer) as Average
FROM t
UNION ALL
SELECT CountryRegionCode as Country, AverageOrdersPerCostumer as Average
FROM table_name
WHERE CountryRegionCode = 'GB';
作为最佳实践,您应该使用UNION ALL
而不是UNION
。
我还注意到你所采取的平均值是平均值。这些国家有不同的规模,因此这可能与不考虑国家的平均数有所不同。