BigQuery:标准SQL和PERCENTILE_CONT()函数

时间:2017-02-03 10:24:13

标签: google-bigquery

我正在尝试使用BigQuery Standard SQL编写查询(因为我们希望利用一些功能)。

在PERCENTILE_CONT()的标准SQL中是否存在等效函数,因为在使用旧版SQL选项时似乎只支持它?

WITH  
  CTE_EmployeeData AS (  
  SELECT  
    E.JobID,  
    E.FB1  
  FROM  
    ZA_Month_End_190.Employee_Primary_Data E  
  WHERE  
    JobID IN UNNEST([111,1414]) /* For these specidifc jobs */  
    ),  
  CTE_Stats AS (  
  SELECT   
    JobID,          
    COUNT(*) SampleSize,        
    SUM(CASE WHEN FB1 > 0 THEN 1 ELSE 0 END) WtdSampleSize_FB1,  
    CAST(SUM(FB1) AS INT64) WtdSum_FB1  
  FROM  
    CTE_EmployeeData  
  GROUP BY   
    JobID  
  ),  
  CTE_Percentiles AS (  
    SELECT  
      ED.JobID,        
      CASE 
        WHEN S.SampleSize > 10 
        THEN PERCENTILE_CONT(0.25) OVER (PARTITION BY ED.JobID ORDER BY ED.FB1 DESC) [Percentile1] 
        ELSE 0 
      END [FB1_Percentile1]        
    FROM         
      CTE_Stats S  
    INNER JOIN CTE_EmployeeData ED ON S.JobID = ED.JobID  
  )  
SELECT
  *
FROM
  CTE_Percentiles

2 个答案:

答案 0 :(得分:3)

BigQuery现在似乎在标准SQL中支持PERCENTILE_CONT(variable, quantile),尽管目前尚未记录。

更新:Now documented - 谢谢Tobi

答案 1 :(得分:0)

另一种选择可能是使用APPROX_QUANTILES();注:这个结果是一系列所需的分位数。

APPROX_QUANTILES([DISTINCT]  expression, number  [{IGNORE|RESPECT} NULLS])

参考:https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#approx_quantiles