高效地计算配置单元或sql中的百分比
现在我想计算欺诈百分比,欺诈标记为0的不同账户数量占账户总数的百分比。我得分两步做。有没有提高效率的建议?最简单的方法是使用一行中的值:高效地计算配置单元或sql中的百分比,sql,hive,Sql,Hive,现在我想计算欺诈百分比,欺诈标记为0的不同账户数量占账户总数的百分比。我得分两步做。有没有提高效率的建议?最简单的方法是使用一行中的值: SELECT (CASE WHEN tag=FRAUD THEN 0 ELSE 1 END) fraud_tag, COUNT(DISTINCT account_id) AS distinct_account_count FROM fraud_tags a GROUP BY (CASE WHEN
SELECT
(CASE WHEN tag=FRAUD THEN 0
ELSE 1 END) fraud_tag,
COUNT(DISTINCT account_id) AS distinct_account_count
FROM fraud_tags a
GROUP BY
(CASE WHEN c.name='riskclass_NotFraud' THEN 0
ELSE 1 END)
RESULT
fraud_tag distinct_account_count
0 100
1 500
SELECT COUNT(DISTINCT case when tag = FRAUD then account_id end) as distinct_fraud,
COUNT(DISTINCT case when tag = FRAUD then NULL else account_id end) as distinct_notfraud,
(COUNT(DISTINCT case when tag = FRAUD then account_id end)*1.0/count(distinct account_id)
) as fraud_rate
FROM fraud_tags ft;