分别计算后如何计算平均值?

时间:2020-01-16 18:05:47

标签: sql amazon-web-services amazon-athena

我想在分别计算之后得出平均值。

这是我当前的查询:

SELECT product_id,
         COUNT(DISTINCT shop_id) AS shops,
         year,
         month,
         day
FROM X
GROUP BY  product_id, year, month, day

但是,现在,我想平均算一下商店。我已经尝试过类似的操作,但是却出现此错误:invalidrequestexception

SELECT product_id,
         AVG(COUNT(DISTINCT shop_id) AS shops) as avg_shops,
         year,
         month,
         day
FROM X
GROUP BY  product_id, year, month, day

2 个答案:

答案 0 :(得分:5)

尝试如下

with cte as
(
SELECT product_id,
         COUNT(DISTINCT shop_id) AS shops,
         year,
         month,
         day
FROM X
GROUP BY  product_id, year, month, day
) select product_id,
         AVG(shops) AS  as avg_shops,
         year,
         month,
         day
FROM cte
GROUP BY  product_id, year, month, day 

答案 1 :(得分:0)

如果您只想要平均值,请使用子查询:

void callbackDispatcher() {
    Workmanager.executeTask((task, inputData) async {

        SQLHelper db = SQLHelper();
        ServerSetting sItem = await db.settingGetItem();
        print(sItem.hidetriggermaintenance);    
        return Future.value(true);
    });
}