![](/img/trans.png)
[英]How to calculate a static average value of all rows in a Table to compare individual row values to in Oracle SQL
[英]How to calculate average of rows in table and add specifc row to input?
也許是這樣的? (如果我錯過了你的觀點,請告訴我):
(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
。
我還注意到你所采取的平均值是平均值。 這些國家有不同的規模,因此這可能與不考慮國家的平均數有所不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.