在BigQuery中两个表具有阈值的SUM聚合

时间:2019-06-20 08:16:21

标签: google-bigquery

下表显示了设备每小时的能耗:

+--------------+-----------+-----------------------+
| energy_usage | device_id |  timestamp            |
+--------------+-----------+-----------------------+
| 10           | 1         |  2019-02-12T01:00:00  |
| 16           | 2         |  2019-02-12T01:00:00  |
| 26           | 1         |  2019-03-12T02:00:00  |
| 24           | 2         |  2019-03-12T02:00:00  |
+--------------+-----------+-----------------------+

我汇总这些数据,以便按白天和设备获取白天和晚上的能耗:

+--------------+------------------+--------------------+-----------+------------+
| energy_usage | energy_usage_day | energy_usage_night | device_id |    date    |
+--------------+------------------+--------------------+-----------+------------+
| 80           | 30               | 50                 | 1         | 2019-06-02 |
| 130          | 60               | 70                 | 2         | 2019-06-03 |
+--------------+------------------+--------------------+-----------+------------+

我只对超过特定阈值的能源使用感兴趣。以下查询对我有用:

WITH temp AS (
  SELECT *, SUM(usage) OVER(win) > 50 qualified,
    SUM(usage) OVER(win) - 50 rolling_sum,
    EXTRACT(HOUR FROM timestamp) BETWEEN 8 AND 19 day_hour,
    EXTRACT(MONTH FROM timestamp) month,
    FORMAT_TIMESTAMP("%Y-%m-%d", timestamp) date
  FROM `project.dataset.table`
  WINDOW win AS (PARTITION BY device_id, TIMESTAMP_TRUNC(timestamp, MONTH) ORDER BY timestamp)
), temp_with_adjustments AS (
  SELECT *, 
    IF(
      ROW_NUMBER() OVER(PARTITION BY device_id, MONTH ORDER BY timestamp) = 1, 
      rolling_sum, 
      usage
    ) AS adjusted_energy_usage
  FROM temp 
  WHERE qualified
)
SELECT ROUND(SUM(adjusted_energy_usage), 4) energy_usage,
  ROUND(SUM(IF(day_hour, adjusted_energy_usage, 0)), 4) energy_usage_day,
  ROUND(SUM(IF(NOT day_hour, adjusted_energy_usage, 0)), 4) energy_usage_night,
  device_id,
  date
FROM temp_with_adjustments
GROUP BY device_id, date

虽然第一个表格显示了能源使用量,但我还有另一个表格显示了相应的能源使用费用:

+--------------+-----------+-----------------------+
| usage_charge | device_id |  timestamp            |
+--------------+-----------+-----------------------+
| 0.2          | 1         |  2019-02-12T01:00:00  |
| 0.6          | 2         |  2019-02-12T01:00:00  |
| 0.1          | 1         |  2019-03-12T02:00:00  |
| 1.2          | 2         |  2019-03-12T02:00:00  |
+--------------+-----------+-----------------------+

对于能源消耗大于50的设备,我想按设备和日期深入了解白天和晚上的使用费用。结果可能如下:

+--------------+------------------+--------------------+--------------+------------------+--------------------+-----------+------------+
| energy_usage | energy_usage_day | energy_usage_night | usage_charge | usage_charge_day | usage_charge_night | device_id |    date    |
+--------------+------------------+--------------------+--------------+------------------+--------------------+-----------+------------+
| 80           | 30               | 50                 | 1.2          | 0.4              | 0.8                | 1         | 2019-06-02 |
| 130          | 60               | 70                 | 2.5          | 1                | 1.5                | 2         | 2019-06-03 |
+--------------+------------------+--------------------+--------------+------------------+--------------------+-----------+------------+

所以我的第一个想法是对使用费使用与对能源使用完全相同的查询。但是,虽然可以为能源使用设定50的阈值,但是我无法为使用费用命名一个固定的阈值,因为费用计算因设备而异。因此,我必须先使能源使用量> 50,然后使用时间戳来汇总使用费。有什么想法可以在bigquery中完成吗?甚至有可能吗?

1 个答案:

答案 0 :(得分:1)

以下内容适用于BigQuery Standard SQL,并且仅基于我在初始查询中看到的应用模式-因此,要我100%确保它正是您所需要的是非常困难的。但是无论如何,从这里开始肯定是个好开始

#standardSQL
WITH temp AS (
  SELECT *, SUM(IF(qualified, usage_charge, 0)) OVER(win) rolling_charge
  FROM (
    SELECT *, SUM(usage) OVER(win) > 50 qualified,
      SUM(usage) OVER(win) - 50 rolling_sum,
      EXTRACT(HOUR FROM timestamp) BETWEEN 8 AND 19 day_hour,
      EXTRACT(MONTH FROM timestamp) month,
      FORMAT_TIMESTAMP("%Y-%m-%d", timestamp) date
    FROM `project.dataset.usage`
    JOIN `project.dataset.charges` USING(device_id, timestamp)
    WINDOW win AS (PARTITION BY device_id, TIMESTAMP_TRUNC(timestamp, MONTH) ORDER BY timestamp)
  )
  WINDOW win AS (PARTITION BY device_id, TIMESTAMP_TRUNC(timestamp, MONTH) ORDER BY timestamp)
), temp_with_adjustments AS (
  SELECT *, 
    IF(
      ROW_NUMBER() OVER(PARTITION BY device_id, MONTH ORDER BY timestamp) = 1, 
      rolling_sum, 
      usage
    ) AS adjusted_energy_usage
  FROM temp 
  WHERE qualified
)
SELECT ROUND(SUM(adjusted_energy_usage), 4) energy_usage,
  ROUND(SUM(IF(day_hour, adjusted_energy_usage, 0)), 4) energy_usage_day,
  ROUND(SUM(IF(NOT day_hour, adjusted_energy_usage, 0)), 4) energy_usage_night,
  ROUND(SUM(rolling_charge), 4) usage_charge,
  ROUND(SUM(IF(day_hour, rolling_charge, 0)), 4) usage_charge_day,
  ROUND(SUM(IF(NOT day_hour, rolling_charge, 0)), 4) usage_charge_night,
  device_id,
  date
FROM temp_with_adjustments
GROUP BY device_id, date