查询以查找在bigquery中一个接一个创建的记录

时间:2019-02-28 21:09:10

标签: google-bigquery

我正在玩bigquery。输入以下内容:

+---------------+---------+---------+--------+----------------------+
|   customer    |  agent  |  value  |  city  |   timestamp          |
+---------------+---------+---------+--------+----------------------+
| 1             | 1       |  106    | LA     |  2019-02-12 03:05pm  |
| 1             | 1       |  251    | LA     |  2019-02-12 03:06pm  |
| 3             | 2       |  309    | NY     |  2019-02-12 06:41pm  |
| 1             | 1       |  654    | LA     |  2019-02-12 05:12pm  |
+---------------+---------+---------+--------+----------------------+

我想查找由一个同一个代理人一次又一次(例如5分钟之内)发出的交易。因此,上表的输出应如下所示:

+---------------+---------+---------+--------+----------------------+
|   customer    |  agent  |  value  |  city  |   timestamp          |
+---------------+---------+---------+--------+----------------------+
| 1             | 1       |  106    | LA     |  2019-02-12 03:05pm  |
| 1             | 1       |  251    | LA     |  2019-02-12 03:06pm  |
+---------------+---------+---------+--------+----------------------+

查询应以某种方式按代理分组并找到此类交易。但是,从输出中可以看出,结果并未真正分组。我的第一个想法是使用LEAD功能,但我不确定。你有什么想法吗?

查询想法:

  • 按业务代表和时间戳DESC排序
  • 从第一行开始,查看下一行(使用LEAD吗?)
  • 检查时间戳差异是否小于5分钟
  • 如果是这样,那么这两行应该在输出中
  • 继续下第二行

当第二行和第三行也符合条件时,第二行将进入输出,这将导致重复的行。我不确定如何避免这种情况。

2 个答案:

答案 0 :(得分:1)

必须有一种更简单的方法,但这能达到您的追求吗?

CTE2 AS (
SELECT customer, agent, value, city, timestamp,
  lead(timestamp,1) OVER (PARTITION BY agent ORDER BY timestamp) timestamp_lead,
  lead(customer,1) OVER (PARTITION BY agent ORDER BY timestamp) customer_lead,
  lead(value,1) OVER (PARTITION BY agent ORDER BY timestamp) value_lead,
  lead(city,1) OVER (PARTITION BY agent ORDER BY timestamp) city_lead,
  lag(timestamp,1) OVER (PARTITION BY agent ORDER BY timestamp) timestamp_lag
FROM CTE
)

SELECT agent, 
  if(timestamp_diff(timestamp_lead,timestamp,MINUTE)<5, concat(cast(customer as string),', ',cast(customer_lead as string)),cast(customer as string)) customer, 
  if(timestamp_diff(timestamp_lead,timestamp,MINUTE)<5, concat(cast(value as string),', ',cast(value_lead as string)),cast(value as string)) value,
  if(timestamp_diff(timestamp_lead,timestamp,MINUTE)<5, concat(cast(city as string),', ',cast(city_lead as string)),cast(city as string)) cities,
  if(timestamp_diff(timestamp_lead,timestamp,MINUTE)<5, concat(cast(timestamp as string),', ',cast(timestamp_lead as string)),cast(timestamp as string)) timestamps
FROM CTE2
WHERE (timestamp_diff(timestamp_lead,timestamp,MINUTE)<5 OR NOT timestamp_diff(timestamp,timestamp_lag,MINUTE)<5)

答案 1 :(得分:0)

以下是用于BigQuery标准SQL

apt-get install openssh-client

您可以使用问题中的示例数据来进行测试,如上示例所示

#standardSQL
SELECT * FROM (
  SELECT *, 
    IF(TIMESTAMP_DIFF(LEAD(ts) OVER(PARTITION BY agent ORDER BY ts), ts, MINUTE) < 5, 
      LEAD(STRUCT(customer AS next_customer, value AS next_value)) OVER(PARTITION BY agent ORDER BY ts), 
    NULL).* 
  FROM `project.dataset.yourtable`
)
WHERE NOT next_customer IS NULL

有结果

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 customer, 1 agent, 106 value,'LA' city, '2019-02-12 03:05pm' ts UNION ALL
  SELECT 1, 1, 251,'LA', '2019-02-12 03:06pm' UNION ALL
  SELECT 3, 2, 309,'NY', '2019-02-12 06:41pm' UNION ALL
  SELECT 1, 1, 654,'LA', '2019-02-12 05:12pm' 
), temp AS (
  SELECT customer, agent, value, city, PARSE_TIMESTAMP('%Y-%m-%d %I:%M%p', ts) ts 
  FROM `project.dataset.table`
)
SELECT * FROM (
  SELECT *, 
    IF(TIMESTAMP_DIFF(LEAD(ts) OVER(PARTITION BY agent ORDER BY ts), ts, MINUTE) < 5, 
      LEAD(STRUCT(customer AS next_customer, value AS next_value)) OVER(PARTITION BY agent ORDER BY ts), 
    NULL).* 
  FROM temp
)
WHERE NOT next_customer IS NULL
-- ORDER BY ts