簡體   English   中英

如何計算表中行的平均值並將特定行添加到輸入?

[英]How to calculate average of rows in table and add specifc row to input?

我有所有國家/地區的平均行,我嘗試計算所有平均值的平均值並添加特定輸入的行。

我能夠通過聚合和平均動作來計算所有國家的平均值,但我不知道如何添加特定的輸入線。

輸入:

在此輸入圖像描述

輸出:

| 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

我還注意到你所采取的平均值是平均值。 這些國家有不同的規模,因此這可能與不考慮國家的平均數有所不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM