乘以sql中的聚合

时间:2014-02-04 06:20:37

标签: sql hive

我正在运行下面的查询

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'中。我想将每周的整体计数乘以欺诈数据,但为什么它不起作用?

2 个答案:

答案 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_ratenum_week的一个实例的account_id之间保持不变,因此您可以在min上使用maxavgb.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;

我没有在此处执行查询。但我猜它应该有用。