Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google bigquery 在BigQuery中查询连续负账户余额天数的金额_Google Bigquery - Fatal编程技术网

Google bigquery 在BigQuery中查询连续负账户余额天数的金额

Google bigquery 在BigQuery中查询连续负账户余额天数的金额,google-bigquery,Google Bigquery,我收集了账户余额: +---------+---------------+---------+------------+ | ID | customer_id | value | timestamp | +---------+---------------+---------+------------+ | 1 | 1 | -200 | 2019-11-18 | | 2 | 1 | 100

我收集了账户余额:

+---------+---------------+---------+------------+
|    ID   |  customer_id  |  value  | timestamp  |
+---------+---------------+---------+------------+
| 1       | 1             |  -200   | 2019-11-18 |
| 2       | 1             |  100    | 2019-11-17 |
| 3       | 1             |  -500   | 2019-11-16 |
| 4       | 1             |  -200   | 2019-11-15 |
| 5       | 2             |  200    | 2019-11-15 |
| 6       | 1             |  0      | 2019-11-14 |
+---------+---------------+---------+------------+
我想得到自上次账户余额为正以来,客户账户余额为负的连续天数。结果应该是这样的:

+---------------+---------------------------------+------------+
|  customer_id  |  Negative account balance since |    Date    |
+---------------+---------------------------------+------------+
| 1             |  1 day                          | 2019-11-18 |
+---------------+---------------------------------+------------+

客户#1有几天是消极的,但是柜台会重新启动,因为2019-11-17是积极的一天。“日期”列显示该客户最近的负帐户余额记录的日期。客户2不在结果中,因为他根本没有消极的日子。如何在BQ中创建这样的查询?

下面是针对BigQuery标准SQL的

#standardSQL
WITH last_positive AS (
  SELECT customer_id, ARRAY_AGG(`timestamp` ORDER BY `timestamp` DESC LIMIT 1)[OFFSET(0)] `timestamp`
  FROM `project.dataset.table`
  WHERE value >= 0
  GROUP BY customer_id
), last_any AS (
  SELECT customer_id, MAX(`timestamp`) `timestamp` 
  FROM `project.dataset.table`
  GROUP BY customer_id
)
SELECT customer_id, DATE_DIFF(a.timestamp, b.timestamp, DAY) days_since, DATE_ADD(b.timestamp, INTERVAL 1 DAY) `timestamp`
FROM last_any a
JOIN last_positive b
USING(customer_id)
WHERE a.timestamp > b.timestamp
如下面的示例所示,是否应用于您问题中的样本数据

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 id, 1 customer_id, -200 value, DATE '2019-11-18' `timestamp` UNION ALL
  SELECT 2, 1, 100, '2019-11-17' UNION ALL
  SELECT 3, 1, -500, '2019-11-16' UNION ALL
  SELECT 4, 1, -200, '2019-11-15' UNION ALL
  SELECT 5, 2, 200, '2019-11-15' UNION ALL
  SELECT 6, 1, 0, '2019-11-14' 
), last_positive AS (
  SELECT customer_id, ARRAY_AGG(`timestamp` ORDER BY `timestamp` DESC LIMIT 1)[OFFSET(0)] `timestamp`
  FROM `project.dataset.table`
  WHERE value >= 0
  GROUP BY customer_id
), last_any AS (
  SELECT customer_id, MAX(`timestamp`) `timestamp` 
  FROM `project.dataset.table`
  GROUP BY customer_id
)
SELECT customer_id, DATE_DIFF(a.timestamp, b.timestamp, DAY) days_since, DATE_ADD(b.timestamp, INTERVAL 1 DAY) `timestamp`
FROM last_any a
JOIN last_positive b
USING(customer_id)
WHERE a.timestamp > b.timestamp
结果是

Row customer_id days_since  timestamp    
1   1           1           2019-11-18