使用标准 sql bigquery 查找数据值中的四分之一值
Finding the quarter value in data values using standard sql bigquery
我想使用标准 sql 将 Q1 和 Q3 作为频率列。
Table 姓名:table.frequency
示例数据:
我做的是:
SELECT (ROUND(COUNT(frequency) *0.25)) AS first_quarter,
(ROUND(COUNT(frequency) *0.75)) AS third_quarter
FROM table
结果和我预期的不一样:
第一季度 = 30577.0
第三季度 = 91730.0
预期结果是频率列的第 1 和第 3 季度值。例子 :
第一季度 = 14
第三节 = 51
有多种方法,但一个简单的方法使用 ntile()
:
select max(case when tile = 1 then frequency end) as q1,
max(case when tile = 2 then frequency end) as q2,
max(case when tile = 3 then frequency end) as q3
from (select t.*, ntile(4) over (order by frequency) as tile
from t
) t;
肯定还有其他方法,例如percentile()
或percentile_cont()
。但这是使用标准 SQL.
的简单方法
Here 是一个 db<>fiddle.
我想使用标准 sql 将 Q1 和 Q3 作为频率列。
Table 姓名:table.frequency
示例数据:
我做的是:
SELECT (ROUND(COUNT(frequency) *0.25)) AS first_quarter,
(ROUND(COUNT(frequency) *0.75)) AS third_quarter
FROM table
结果和我预期的不一样:
第一季度 = 30577.0 第三季度 = 91730.0
预期结果是频率列的第 1 和第 3 季度值。例子 : 第一季度 = 14 第三节 = 51
有多种方法,但一个简单的方法使用 ntile()
:
select max(case when tile = 1 then frequency end) as q1,
max(case when tile = 2 then frequency end) as q2,
max(case when tile = 3 then frequency end) as q3
from (select t.*, ntile(4) over (order by frequency) as tile
from t
) t;
肯定还有其他方法,例如percentile()
或percentile_cont()
。但这是使用标准 SQL.
Here 是一个 db<>fiddle.