在CASE语句的不同部分重用汇总计算

时间:2019-04-16 04:43:14

标签: google-bigquery case aggregate-functions

我有一个表myTable,其中的单列表包含一个表示单个字节值的整数,如下所示:

status_code
-----------
(single bit values:)
1
2
4
8
(multiple bit values:)
...
12
3
etc.

我正在使用类似于以下内容的逻辑,根据与某个值匹配的位的总比例选择总体“状态”。如您所见,在重复执行一些相同的计算时,存在一些重复:

select 
       (case when (sum(case when status_code >> 4 = 1 then 1 else 0 end) / count(*)) + 
                  (sum(case when status_code >> 3 = 1 then 1 else 0 end) / count(*)) > 0.75
             then 0
       else 
          case when (sum(case when status_code >> 4 = 1 then 1 else 0 end) / count(*)) + 
                    (sum(case when status_code >> 3 = 1 then 1 else 0 end) / count(*)) +
                    (sum(case when status_code >> 2 = 1 then 1 else 0 end) / count(*)) > 0.75
               then 1
          else 
               case when (sum(case when status_code >> 4 = 1 then 1 else 0 end) / count(*)) + 
                         (sum(case when status_code >> 3 = 1 then 1 else 0 end) / count(*)) +
                         (sum(case when status_code >> 2 = 1 then 1 else 0 end) / count(*)) +
                         (sum(case when status_code >> 1 = 1 then 1 else 0 end) / count(*)) > 0.75
                    then 2
               else 
                 3
               end
          end
        end) as status 
from myTable

如何通过重新使用每个状态的计算比例来简化查询(可能使其性能更高)?例如,我怎么可能别名以下内容并在case语句的不同部分中重复使用?

(sum(case when status_code >> 4 = 1 then 1 else 0 end) / count(*))

那有可能吗?

1 个答案:

答案 0 :(得分:1)

  

如何简化查询?

以下是我的选择之一

    
#standardSQL
CREATE TEMP FUNCTION f(code INT64, bits INT64) AS (
  IF(code >> bits = 1, 1, 0)
);
SELECT (CASE 
  WHEN (SUM(f(status_code, 4)) + 
    SUM(f(status_code, 3))) / COUNT(*) > 0.75 THEN 0
  WHEN (SUM(f(status_code, 4))  + 
    SUM(f(status_code, 3))  +
    SUM(f(status_code, 2))) / COUNT(*) > 0.75 THEN 1
  WHEN (SUM(f(status_code, 4)) + 
    SUM(f(status_code, 3)) +
    SUM(f(status_code, 2)) +
    SUM(f(status_code, 1))) / COUNT(*) > 0.75 THEN 2
  ELSE 3
  END) AS status 
FROM `project.dataset.table`

或进一步的“重构”给出:

#standardSQL
CREATE TEMP FUNCTION f(code INT64, bits INT64) AS (
  IF(code >> bits = 1, 1, 0)
);
SELECT 
  (CASE 
    WHEN SUM(f(status_code, 4) + f(status_code, 3)) / COUNT(*) > 0.75 THEN 0
    WHEN SUM(f(status_code, 4) + f(status_code, 3) + f(status_code, 2)) / COUNT(*) > 0.75 THEN 1
    WHEN SUM(f(status_code, 4) + f(status_code, 3) + f(status_code, 2) + f(status_code, 1)) / COUNT(*) > 0.75 THEN 2
    ELSE 3
  END) AS status 
FROM `project.dataset.table`
  

可能使其性能更高...

所以,最终

#standardSQL
CREATE TEMP FUNCTION f(code INT64, bits INT64) AS (
  IF(code >> bits = 1, 1, 0)
);
SELECT 
  (CASE 
    WHEN f4 + f3 > 0.75 THEN 0
    WHEN f4 + f3 + f2 > 0.75 THEN 1
    WHEN f4 + f3 + f2 + f1 > 0.75 THEN 2
    ELSE 3
  END) AS status 
FROM (
  SELECT 
    SUM(f(status_code, 4))/ COUNT(*) f4, 
    SUM(f(status_code, 3))/ COUNT(*) f3, 
    SUM(f(status_code, 2))/ COUNT(*) f2, 
    SUM(f(status_code, 1))/ COUNT(*) f1 
  FROM `project.dataset.table`
)