Skip to content

These allow you to override the PARTITION BY and ORDER BY clauses of window functions generated by grouped mutates.

Usage

window_order(.data, ...)

window_frame(.data, from = -Inf, to = Inf)

Arguments

.data

A lazy data frame backed by a database query.

...

Variables to order by

from, to

Bounds of the frame.

Examples

library(dplyr, warn.conflicts = FALSE)

db <- memdb_frame(g = rep(1:2, each = 5), y = runif(10), z = 1:10)
db %>%
  window_order(y) %>%
  mutate(z = cumsum(y)) %>%
  show_query()
#> <SQL>
#> SELECT `g`, `y`, SUM(`y`) OVER (ORDER BY `y` ROWS UNBOUNDED PRECEDING) AS `z`
#> FROM `dbplyr_qJw1aCGFvN`

db %>%
  group_by(g) %>%
  window_frame(-3, 0) %>%
  window_order(z) %>%
  mutate(z = sum(y)) %>%
  show_query()
#> <SQL>
#> SELECT
#>   `g`,
#>   `y`,
#>   SUM(`y`) OVER (PARTITION BY `g` ORDER BY `z` ROWS 3 PRECEDING) AS `z`
#> FROM `dbplyr_qJw1aCGFvN`