Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
高效地计算配置单元或sql中的百分比_Sql_Hive - Fatal编程技术网

高效地计算配置单元或sql中的百分比

高效地计算配置单元或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

现在我想计算欺诈百分比,欺诈标记为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 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;