我正在运行下面的查询
SELECT
a.num_week,
CAST(COUNT(distinct a.account_id)*b.fraud_rate as BIGINT) as unpaid_other
FROM ads_weekly_total_unpaid_other a
JOIN ads_weekly_unpaid_other_fraud_rate b
ON a.num_week = b.num_week
GROUP BY a.num_week
我得到了以下错误,而不是在GROUP BY键'fraud_rate'中。我想将每周的整体计数乘以欺诈数据,但为什么它不起作用?
答案 0 :(得分:0)
SELECT z.num_week,CAST(countId * y.fraud_rate) as unpaid_other
FROM (
SELECT a.num_week,COUNT(distinct a.account_id) as countId
FROM ads_weekly_total_unpaid_other a
JOIN ads_weekly_unpaid_other_fraud_rate b
ON a.num_week = b.num_week
GROUP BY a.num_week
)z
JOIN ads_weekly_unpaid_other_fraud_rate y
ON y.num_week=z.num_week
答案 1 :(得分:0)
在您的查询中,您只在a.num_week
上进行分组,您在COUNT(Distinct
上使用了汇总函数a.account_id
,但您在b.fraud_rate
上没有任何汇总函数,它都不是按键组,因此错误。由于fraud_rate
在num_week
的一个实例的account_id之间保持不变,因此您可以在min
上使用max
,avg
,b.fraud_rate
等任何汇总函数它会给你相同的结果。
SELECT a.num_week,
CAST((COUNT(distinct a.account_id)* MAX(b.fraud_rate)) as BIGINT) as unpaid_other
FROM ads_weekly_total_unpaid_other a
JOIN ads_weekly_unpaid_other_fraud_rate b
ON a.num_week = b.num_week
GROUP BY a.num_week;
我没有在此处执行查询。但我猜它应该有用。