有没有办法在下面的公式中使用变量作为动态数字,所以当对手在id列中更改时,我可以将4game_so_ct数重置为零。我也尝试按id排序,只是明确地将它们全部按顺序排列,并且不重置数字。第4game列是前面要查看的行数,以获取id相同的前一行数。
sum(strikeouts) over (order by game_dt rows between 3 preceding and current row) as 4game_so_ct
我尝试了什么
sum(strikeouts) over (order by game_dt rows between 4game preceding and current row) as 4game_so_ct
但它明显错误。
我有一张如下表格
GAME_ID GAME_DT HOME_TEAM_ID AWAY_TEAM_ID id strikeouts 4game test 4game_so_ct
WS1192204120 19220412 WS1 NYA NYAWS1 4 0 0 4
BOS192204180 19220418 BOS NYA NYABOS 2 0 NYAWS1 2
NYA192204210 19220421 NYA WS1 WS1NYA 1 0 NYABOS 1
NYA192204220 19220422 NYA WS1 WS1NYA 2 1 WS1NYA 2
NYA192204230 19220423 NYA WS1 WS1NYA 0 2 WS1NYA 0
NYA192204250 19220425 NYA PHA PHANYA 1 0 WS1NYA 1
NYA192204260 19220426 NYA PHA PHANYA 4 1 PHANYA 4
NYA192204280 19220428 NYA BOS BOSNYA 0 0 PHANYA 0
NYA192204290 19220429 NYA BOS BOSNYA 6 1 BOSNYA 6
NYA192204300 19220430 NYA BOS BOSNYA 4 2 BOSNYA 4
NYA192205010 19220501 NYA BOS BOSNYA 2 3 BOSNYA 2
NYA192205020 19220502 NYA BOS BOSNYA 1 4 BOSNYA 1
我希望在4game_so_ct列
中看到什么GAME_ID GAME_DT HOME_TEAM_ID AWAY_TEAM_ID id strikeouts 4game test 4game_so_ct
WS1192204120 19220412 WS1 NYA NYAWS1 4 0 0 4
BOS192204180 19220418 BOS NYA NYABOS 2 0 NYAWS1 2
NYA192204210 19220421 NYA WS1 WS1NYA 1 0 NYABOS 1
NYA192204220 19220422 NYA WS1 WS1NYA 2 1 WS1NYA 3
NYA192204230 19220423 NYA WS1 WS1NYA 0 2 WS1NYA 3
NYA192204250 19220425 NYA PHA PHANYA 1 0 WS1NYA 1
NYA192204260 19220426 NYA PHA PHANYA 4 1 PHANYA 5
NYA192204280 19220428 NYA BOS BOSNYA 0 0 PHANYA 0
NYA192204290 19220429 NYA BOS BOSNYA 6 1 BOSNYA 6
NYA192204300 19220430 NYA BOS BOSNYA 4 2 BOSNYA 10
NYA192205010 19220501 NYA BOS BOSNYA 2 3 BOSNYA 12
NYA192205020 19220502 NYA BOS BOSNYA 1 4 BOSNYA 13
答案 0 :(得分:1)
你过度复杂了。在partition by
中使用SUM
,这是预期的结果。
sum(strikeouts) over (partition by id order by game_dt)
答案 1 :(得分:0)
语法图可能有点难以理解。但是documentation很清楚窗框规范需要是一个常数:
<window frame preceding> ::= { UNBOUNDED PRECEDING | <unsigned_value_specification> PRECEDING | CURRENT ROW } . . . <unsigned value specification> ::= { <unsigned integer literal> }
因此,使用Windows框架规范无法实现所需。