在当前的行前进和精确的匹配之间的行

时间:2018-03-16 13:17:10

标签: sql google-bigquery window-functions

考虑以下虚拟数据集

ID EVENT VALUE SORT_KEY
1 submitted 10 1
1 action 20 2
1 closed 30 3
1 action 30 4
2 submitted 10 1 
2 action 10 2
2 action 10 3
2 closed 10 4
2 action 10 5
3 action 29 1
3 submitted 20 2
3 action 10 3
3 closed 10 4
3 action 10 5
4 action 10 1

我想总结提交(包含)和关闭之间每个ID的所有操作。我不关心这些界限之外的事件。 我想知道是否有可能通过id构建一个窗口函数分区并跟随一个匹配表达式。

期望的结果:

ID EVENT VALUE_SUM
1 submitted 60
2 submitted 40
3 submitted 40

计算此查询的查询类似于以下内容:

SELECT 
    id
  , event
  , SUM(value) OVER (PARTITION BY id ROWS BETWEEN CURRENT ROW PRECEDING AND event='closed' FOLLOWING) as value_sum
FROM my_table
WHERE event = 'submitted'

我知道也许可以通过多个连接来实现这一点,但是由于数据的大小和优化原因,我想知道是否可以使用窗口函数来实现这一点。感谢。

1 个答案:

答案 0 :(得分:3)

下面是BigQuery SQL 没有JOIN但仍然只是快速草图所以可能仍然是重构/优化的选项

#standardSQL
SELECT id, SUM(VALUE) AS val
FROM (
  SELECT id, EVENT, VALUE, 
    SUM(boundary) OVER(PARTITION BY ID ORDER BY SORT_KEY ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) grp
  FROM (
    SELECT *, 
      COUNTIF(EVENT IN ('submitted', 'closed')) OVER(PARTITION BY ID, EVENT ORDER BY SORT_KEY) boundary
    FROM `project.dataset.table` t
  )
)
WHERE grp = 1
OR (grp = 2 AND EVENT = 'closed')
GROUP BY ID

您可以使用您的问题中的虚拟数据来测试/播放它:

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 ID, 'submitted' EVENT, 10 VALUE, 1 SORT_KEY UNION ALL
  SELECT 1, 'action', 20, 2 UNION ALL
  SELECT 1, 'closed', 30, 3 UNION ALL
  SELECT 1, 'action', 30, 4 UNION ALL
  SELECT 2, 'submitted', 10, 1 UNION ALL 
  SELECT 2, 'action', 10, 2 UNION ALL
  SELECT 2, 'action', 10, 3 UNION ALL
  SELECT 2, 'closed', 10, 4 UNION ALL
  SELECT 2, 'action', 10, 5 UNION ALL
  SELECT 3, 'action', 29, 1 UNION ALL
  SELECT 3, 'submitted', 20, 2 UNION ALL
  SELECT 3, 'action', 10, 3 UNION ALL
  SELECT 3, 'closed', 10, 4 UNION ALL
  SELECT 3, 'action', 10, 5 UNION ALL
  SELECT 4, 'action', 10, 1 
)
SELECT id, SUM(VALUE) AS val
FROM (
  SELECT id, EVENT, VALUE, 
    SUM(boundary) OVER(PARTITION BY ID ORDER BY SORT_KEY ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) grp
  FROM (
    SELECT *, 
      COUNTIF(EVENT IN ('submitted', 'closed')) OVER(PARTITION BY ID, EVENT ORDER BY SORT_KEY) boundary
    FROM `project.dataset.table` t
  )
)
WHERE grp = 1
OR (grp = 2 AND EVENT = 'closed')
GROUP BY ID
ORDER BY ID

,结果是

Row id  val  
1   1   60   
2   2   40   
3   3   40