DolphinDB:如何计算表中两行之间的差异?

huangapple go评论74阅读模式
英文:

DolphinDB: How to calculate the difference between two rows in a table?

问题

我有一张包含四列“order_book_id”、“date”、“Q”和“revenue”的表格。我想执行以下操作:

  1. 根据“order_book_id”和“date”对表格进行分组。

  2. 验证在每个分组内列“Q”的值是否等于1(在每个分组内值相同)。

    • 对于满足条件的组,返回最新的行。

    • 对于不满足条件的组,计算最后两行之间的差异。

这是我的脚本:

result = select date, order_book_id, iif(Q == 1, revenue, deltas(revenue)) as revenue from tmp context by date, order_book_id order by order_book_id asc limit -1;

目前,我正在使用iif(Q == 1, revenue, deltas(revenue))来实现所需的逻辑。为了分别计算多列的差异,必须为每列单独确定iff条件。

在DolphinDB中是否有更有效的函数或方法?提前感谢。

英文:

I have a table containing the four columns “order_book_id”, “date”, “Q”, and “revenue”. I want to perform the following operations:

  1. Group the table by “order_book_id” and “date”.

  2. Verify if the values of column "Q" are equal to 1 within each group (the values are same within each group)

  • For groups where the condition is met, return the latest row.

  • For groups where the condition is not met, calculate the difference between the two latest rows.

Here is my script:

result = select date, order_book_id, iif(Q == 1, revenue, deltas(revenue)) as revenue from tmp context by date, order_book_id order by order_book_id asc limit -1;

Currently, I'm using iif(Q == 1, revenue, deltas(revenue)) to achieve the desired logic. To calculate the differences for multiple columns separately, the iff condition must be determined for each column individually.

Is there a more efficient function or method in DolphinDB? Thanks in advance.

答案1

得分: 1

以下是翻译好的部分:

您可以参考以下脚本

order_book_id = `a`b`a`b`c`a`c`a`b
date = take(2022.01.01, 9)
Q = 1 0 1 0 1 1 1 1 0
revenue = 1 3 2 5 9 2 1 6 2
t = table(order_book_id, date, Q, revenue)
defg f(revenue){ return last(revenue) - last(prev(revenue))}
// 或 defg f(revenue){ return last(deltas(revenue))} 
// 或 defg f(revenue){ return revenue[revenue.size()-1] - revenue[revenue.size()-2] }

select iif(last(Q)==1, last(revenue), f(revenue)) from t group by date, order_book_id

要处理多列您可以尝试元编程

order_book_id =  `a`b`a`b`c`a`c`a`b
date = take(2022.01.01, 9)
Q = 1 0 1 0 1 1 1 1 0
revenue = 1 3 2 5 9 2 1 6 2
revenue1 = 1 3 2 5 9 2 1 6 2
revenue2 = 1 3 2 5 9 2 1 6 2
t = table(order_book_id, date, Q, revenue, revenue1, revenue2)

defg f(Q,x): iif(last(Q)==1, last(x), last(x)-last(prev(x)))
col_list = [`revenue, `revenue1, `revenue2]
sql(select=sqlColAlias(eachRight(makeCall{f}, sqlCol(`Q), sqlCol(col_list)), `a`b`c), from=t, groupBy=sqlCol([`date, `order_book_id]), orderBy=sqlCol(`order_book_id)).eval()
英文:

You can refer to the following scripts:

order_book_id =  `a`b`a`b`c`a`c`a`b
date = take(2022.01.01, 9)
Q = 1 0 1 0 1 1 1 1 0
revenue = 1 3 2 5 9 2 1 6 2
t = table(order_book_id, date, Q, revenue)
defg f(revenue){ return last(revenue) - last(prev(revenue))}
// or defg f(revenue){ return last(deltas(revenue))} 
// or defg f(revenue){ return revenue[revenue.size()-1] - revenue[revenue.size()-2] }

select iif(last(Q)==1, last(revenue), f(revenue)) from t group by date, order_book_id

To deal with multiple columns, you can try metaprogramming:

order_book_id =  `a`b`a`b`c`a`c`a`b
date = take(2022.01.01, 9)
Q = 1 0 1 0 1 1 1 1 0
revenue = 1 3 2 5 9 2 1 6 2
revenue1 = 1 3 2 5 9 2 1 6 2
revenue2 = 1 3 2 5 9 2 1 6 2
t = table(order_book_id, date, Q, revenue, revenue1, revenue2)

defg f(Q,x): iif(last(Q)==1, last(x), last(x)-last(prev(x)))
col_list = [`revenue, `revenue1, `revenue2]
sql(select=sqlColAlias(eachRight(makeCall{f}, sqlCol(`Q), sqlCol(col_list)), `a`b`c), from=t, groupBy=sqlCol([`date, `order_book_id]), orderBy=sqlCol(`order_book_id)).eval()

huangapple
  • 本文由 发表于 2023年8月9日 14:18:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865057-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定