簡體   English   中英

使用 Javascript 從 BigQuery 中的上一行和同一列計算

[英]calculate from previous row in BigQuery and same column using Javascript

我需要從以前的原始數據中計算pending_principal,有什么我可以直接在SQL 中執行此操作的嗎? 我正在使用 BigQuery 和 Javascript。

樣本數據

日期 生麻 滯納金 興趣 Pending_principal
2020-01-01 1 0 100000 1000000
2020-01-02 2 null 150000 null
2020-01-03 3 null 200000 null
2020-01-04 4 null 250000 null
2020-01-05 1 100000 300000 1000000
2020-01-06 2 null 900000 null

我想計算包含 null 值的 pending_principal 和 late_fee

如果 rownumb=1 滯納金已經存在於表中,late_fee 的邏輯是:

late_fee=5% * 上一行pending_principal

如果 rownumb=1 pending_principal 已存在於表中,則 pending_principal 的邏輯,但如果 rownum 不為 1,則邏輯為:

pending_principal=上一個pending_principal+late_fee+利息

例如在 2020-01-02

遲到費=5%*1.000.000=50.000

掛起的_principal=1.000.000+50.000+150.000=1.200.000

我寫了查詢:

CREATE TEMP FUNCTION udf_calc(x ARRAY<STRUCT<rownum INT64, late_fee INT64, interest INT64, pending_principal INT64>>)
RETURNS STRUCT<rownum INT64,late_fee INT64, interest INT64, pending_principal INT64>
LANGUAGE js
AS """
  var vrownum = 0;
  var vlate_fee = 0;
  var vinterest = 0;
  var vpending_principal = 0;
  for (var row of x)
  {
    if (vrownum == 1) {
      vlate_fee=row.late_fee
    }
    else {vlate_fee = parseInt(vpending_principal) * 0.05}
    ;
    if (vrownum === 1) {
      vpending_principal = row.pending_principal;
    }
    else {
      vpending_principal = parseInt(vpending_principal) + parseInt(vlate_fee) + parseInt(row.interest);
    }
    vinterest = row.interest;
    vrownum = row.rownum;
  }
  r = {rownum:vrownum,late_fee:vlate_fee, interest:vinterest, pending_principal:vpending_principal};
  return r;
""";

WITH mytable AS (
  SELECT date '2020-01-01' as date, 1 as rownum , 0 as late_fee, 100000 as interest, 1000000 as pending_principal UNION ALL
  SELECT date '2020-01-02',2 , null, 150000, null UNION ALL
  SELECT date '2020-01-03',3 , null, 200000, null UNION ALL
  SELECT date '2020-01-04',4 ,null, 250000, null UNION ALL
  SELECT date '2020-01-05',1 , 100000 , 300000, 100000 UNION ALL
  SELECT date '2020-01-06',2 ,  null, 900000, null
)
select date,
  udf_calc(array_agg(STRUCT(rownum, late_fee, interest, pending_principal)) over (order by date rows unbounded preceding)).*
from mytable

但結果不正確

日期 行數 滯納金 興趣 Pending_principal
2020-01-01 1 0 0 1000000
2020-01-02 2 50000 150000 120萬
2020-01-03 3 60000 200000 1460000
2020-01-04 4 73000 250000 1783000
2020-01-05 1 89150 300000 2172150
2020-01-06 2 108608 900000 3180757

我希望結果是

日期 行數 滯納金 興趣 Pending_principal
2020-01-01 1 0 0 1000000
2020-01-02 2 50000 150000 120萬
2020-01-03 3 60000 200000 1460000
2020-01-04 4 73000 250000 1783000
2020-01-05 1 100000 300000 1000000
2020-01-06 2 50000 900000 1950000

如果 rownum==1,我認為我的腳本沒有讀取條件

這在某種程度上可能嗎?

使用group_num類的東西更容易將行分成組:

CREATE TEMP FUNCTION udf_calc(x ARRAY<STRUCT<late_fee INT64, interest INT64, pending_principal INT64>>)
RETURNS STRUCT<late_fee INT64, interest INT64, pending_principal INT64>
LANGUAGE js
AS """
  var vlate_fee = 0;
  var vinterest = 0;
  var vpending_principal = 0;
  for (var row of x)
  {
    vlate_fee = parseInt(vpending_principal) * 0.05;
    if (vpending_principal === 0) {
      vpending_principal = row.pending_principal;
    }
    else {
      vpending_principal = parseInt(vpending_principal) + parseInt(vlate_fee) + parseInt(row.interest);
    }
    vinterest = row.interest;
  }
  r = {late_fee:vlate_fee, interest:vinterest, pending_principal:vpending_principal};
  return r;
""";

WITH mytable AS (
  SELECT date '2020-01-01' as date, 1 as group_num , 0 as late_fee, 100000 as interest, 1000000 as pending_principal UNION ALL
  SELECT date '2020-01-02',1 , null, 150000, null UNION ALL
  SELECT date '2020-01-03',1 , null, 200000, null UNION ALL
  SELECT date '2020-01-04',1 ,null, 250000, null UNION ALL
  SELECT date '2020-01-05',2 , 100000 , 300000, 1000000 UNION ALL
  SELECT date '2020-01-06',2 ,  null, 900000, null
)
select date,
  udf_calc(array_agg(STRUCT(late_fee, interest, pending_principal)) over (partition by group_num order by date rows unbounded preceding)).*
from mytable

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM