计算每个相关值在每个季度的平均值

时间:2019-04-04 19:36:00

标签: sql google-bigquery

我在BigQuery中有一个查询。我想知道四分之一以上的平均值。在我当前的SQL中,id1的Q1期间的值与id2相同。

这就是我所拥有的,而且价值很好:

row|averages|quarter|identifier
-----------------------------
1  | 10     | 1     | id1
2  | 20     | 2     | id1
3  | 30     | 1     | id2
4  | 40     | 2     | id2

以下是我为上述结构编写的SQL,它具有良好的价值:

WITH
  index_cal AS (
  SELECT
    values-01,
    kind,
    EXTRACT (QUARTER  FROM  date) as QUARTER,
    date,
  FROM
    `project.dataset.table`,
  geom AS (
  SELECT
    identifier
  FROM
    `project.dataset.table2` )
SELECT
   AVG(values-01) AS averages,
    QUARTER AS quarter,
    geom. identifier as identifier
  FROM
    index_cal as g
INNER JOIN
  geom
ON
  INTERSECTS(g.kind,
    geom. identifier)
GROUP BY
  identifier
  quarter

我想要对每个标识符的每个季度的值进行分组,以便每个标识符只有1个关联行:

row | averages | quarter | identifier
----------------------------------
1   | 10       | 1       | id1
    | 20       | 2       |
----------------------------------
2   | 30       | 1       | id2
    | 40       | 2       |
----------------------------------

要获得所需的结构,以使id1仅具有1个关联行,并且对于所有标识符同样如此,我编写了以下SQL查询:

WITH
  index_cal AS (
  SELECT
    values-01,
    kind,
    EXTRACT (QUARTER  FROM  date) as QUARTER,
    date,
  FROM
    `project.dataset.table`,
  geom AS (
  SELECT
    identifier
  FROM
    `project.dataset.table2` )
SELECT
  ARRAY(
  SELECT
    AS STRUCT AVG(values-01) AS averages,
    QUARTER AS quarter
  FROM
    index_cal
  GROUP BY
    QUARTER ) as INDEX,
  geom. identifier as identifier
FROM
  index_cal AS g
INNER JOIN
  geom
ON
  INTERSECTS(g.kind,
    geom. identifier)
GROUP BY
  identifier

在运行此查询时,我获得了按季度分组的所有标识符的平均值,从而对所有标识符重复了值(例如,本例中为15和25):

row | averages | quarter | identifier
----------------------------------
1   | 15       | 1       | id1
    | 25       | 2       |
----------------------------------
2   | 15       | 1       | id2
    | 25       | 2       |
----------------------------------
2   | 15       | 1       | id3
    | 25       | 2       |
----------------------------------

最后,我想回答的是基于values-01的季度间隔内identifier的平均值。当前,对于identifier的任何值,它们都是相同的。

1 个答案:

答案 0 :(得分:1)

在给出原始值的原始查询中使用ARRAY_AGG解决了该问题

initLegends();
initValues();