清点清楚后如何计算平均值?
How to calculate the average after counting distinctly?
我想清点后取平均值。
这是我当前的查询:
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
试试下面的方法
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
如果您只想要平均值,请使用子查询:
SELECT AVG(shops)
FROM (SELECT product_id, COUNT(DISTINCT shop_id) AS shops
FROM X
GROUP BY product_id, year, month, day
) x
我想清点后取平均值。
这是我当前的查询:
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
试试下面的方法
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
如果您只想要平均值,请使用子查询:
SELECT AVG(shops)
FROM (SELECT product_id, COUNT(DISTINCT shop_id) AS shops
FROM X
GROUP BY product_id, year, month, day
) x