简体   繁体   English

BigQuery标准SQL:如何获取两列之间的差异中位数?

[英]BigQuery Standard SQL: How to get median of difference between two columns?

How can I get the median difference after subtracting two columns in a table after I set the dialect to Standard SQL ? 将方言设置为Standard SQL后,如何在减去表中的两列后如何获得中间差?

These are the things that I have tried and failed with: 这些是我尝试过并失败的事情:

SELECT
  symbol,
  median(max_height-min_height)
FROM
  `body_table-1345.bodies.heights`
WHERE
  symbol = 'mouse_1344'
  AND max_height> 0
  AND min_height> 0
  AND (measure_date> '2017-11-01 08:45:00.000 UTC'
    AND measure_date< '2017-11-30 17:00:00.000 UTC')
GROUP BY
  symbol

but I get the error Error: Function not found: median at [3:3] 但出现Error: Function not found: median at [3:3]

The following works with Legacy SQL but not with Standard SQL : 以下内容适用于Legacy SQL但不适用于Standard SQL

SELECT
  symbol,
  NTH(501, QUANTILES(max_height-min_height, 1001))
FROM
  `body_table-1345.bodies.heights`
WHERE
  symbol = 'mouse_1344'
  AND max_height> 0
  AND min_height> 0
  AND (measure_date> '2017-11-01 08:45:00.000 UTC'
    AND measure_date< '2017-11-30 17:00:00.000 UTC')
GROUP BY
  symbol

The columns I'd like to subtract are max_height and min_height . 我想减去的列是max_heightmin_height

Instead, you can use a slight variation of ANSI standard percentile functions. 相反,您可以使用ANSI标准百分位数函数的细微变化。

SELECT DISTINCT symbol,
       percentile_cont(max_height-min_height, 0.5) over (partition by symbol )
FROM `body_table-1345.bodies.heights`
WHERE symbol = 'mouse_1344' AND
      max_height > 0 AND
      min_height > 0 AND
      (measure_date> '2017-11-01 08:45:00.000 UTC' AND
       measure_date< '2017-11-30 17:00:00.000 UTC'
      );

Unfortunately, this is one of the analytic functions that is only available as an analytic function and not as an aggregation function. 不幸的是,这是分析功能之一,只能用作分析功能而不能用作聚合功能。 Hence, the SELECT DISTINCT syntax rather than GROUP BY . 因此,使用SELECT DISTINCT语法而不是GROUP BY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM