I have a table containing the four columns “order_book_id”, “date”, “Q”, and “revenue”. I want to perform the following operations:
Group the table by “order_book_id” and “date”.
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.