Window function based on transition of a column value

 

问题

https://stackoverflow.com/questions/70494696/window-function-based-on-transition-of-a-column-value

I have response query like below

dest

emp

893106

0

717205

1

888305

0

312301

1

645100

0

222001

0

761104

1

And I want to get window function to separate rows like below:

dest

emp

893106

0

717205

1


dest

emp

888305

0

312301

1


dest

emp

645100

0

222001

0

761104

1

So each window has to begin withempvalue = 0 and end withempvalue = 1. It has to detect a transition of a column value.

解答

sth有序的数据,每当上一条记录的emp1时,将数据分组。用SQL解决这个问题就非常麻烦了,需要先创建行号,再按要求创建标识列,最后利用标识列与行号才能完成按条件分组。通常的办法是读出来用PythonSPL来做, SPL(一种 Java 的开源包)更容易被Java应用集成,代码也更简单一点,只要两句:


A

1

=PG.query("select dest,emp from t3 order by sth")

2

=A1.group@i(emp[-1]==1)

SPL源代码:https://github.com/SPLWare/esProc

问答搜集