bigquery - 计算月度未偿付价值

时间:2018-03-13 15:29:58

标签: google-bigquery

我试图解决以下问题:

用户花了三笔贷款,运行时间分别为3,4和5个月。 如何计算BigQuery每个时间点,他拥有多少?

我知道要在RPython中进行此计算,但显然更喜欢BigQuery/SQL解决方案。

谢谢!

我有数据:

Take Date   Return Date   Sum
2016-01-01  2016-03-31    10
2016-02-01  2016-05-31    20
2016-03-01  2016-07-31    50

我需要这样的输出:

Date        Sum
2016-01-01  10
2016-02-01  30
2016-03-01  80
2016-04-01  70
2016-05-01  70
2016-06-01  50
2016-07-01  50
2016-08-01   0

1 个答案:

答案 0 :(得分:3)

以下是BigQuery Standard SQL

   
#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 id, DATE '2016-01-01' take_date, DATE '2016-03-31' return_date, 10 amount 
  UNION ALL SELECT 1, DATE '2016-02-01', DATE '2016-05-31', 20 
  UNION ALL SELECT 1, DATE '2016-03-01', DATE '2016-07-31', 50 
), dates AS (
  SELECT id, day
  FROM (
    SELECT id, GENERATE_DATE_ARRAY(
        MIN(take_date), 
        DATE_ADD(DATE_TRUNC(MAX(return_date), MONTH), INTERVAL 1 MONTH), 
        INTERVAL 1 MONTH
      ) days
    FROM `project.dataset.table`
    GROUP BY id
  ), UNNEST(days) day
)
SELECT d.id, d.day, SUM(IF(d.day BETWEEN t.take_date AND t.return_date, amount, 0)) amount
FROM dates d 
LEFT JOIN `project.dataset.table` t
ON d.id = t.id
GROUP BY d.id, d.day
ORDER BY d.day  

结果为

Row id  day         amount   
1   1   2016-01-01  10   
2   1   2016-02-01  30   
3   1   2016-03-01  80   
4   1   2016-04-01  70   
5   1   2016-05-01  70   
6   1   2016-06-01  50   
7   1   2016-07-01  50   
8   1   2016-08-01  0