You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using n() in an ordered window frame the ordering seems to be ignored when it's translated to SQL. Use case would be knowing how many rows are actually included in a rolling sum/average/etc.
mf<- memdb_frame(
tibble(
grp= c(rep('a',4), rep('b', 5)),
order= seq(1,9),
val= seq(11,19)
)
)
mf %>%
group_by(
grp
) %>%
# Arrange by order
window_order(order) %>%
# Calc rolling 2 row sum
window_frame(-2,-1) %>%
mutate(
roll_last_2_sum= sum(val),
# Get number of rows in framen_rows_in_frame= n()
) %>%
show_query()
# Output is:# SELECT `grp`, `order`, `val`, SUM(`val`) OVER (PARTITION BY `grp` ORDER BY `order` ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) AS `roll_last_2_sum`, COUNT(*) OVER (PARTITION BY `grp`) AS `n_rows_in_frame`# Output should be:# SELECT `grp`, `order`, `val`, SUM(`val`) OVER (PARTITION BY `grp` ORDER BY `order` ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) AS `roll_last_2_sum`, COUNT(*) OVER (PARTITION BY `grp` ORDER BY `order` ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) AS `n_rows_in_frame`
The text was updated successfully, but these errors were encountered:
When using
n()
in an ordered window frame the ordering seems to be ignored when it's translated to SQL. Use case would be knowing how many rows are actually included in a rolling sum/average/etc.The text was updated successfully, but these errors were encountered: