-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
window function could use local sort #3619
Comments
Hi @jangorecki, Great to see that you are trying out PRQL and posting questions. You can put the So for your example: library(prqlr)
d = data.frame(id=c(1,2,3,6,5,4), value=c(2,3,4,2,3,4))
sql = prql_compile("from d | window rolling:3 (sort id | derive {sma3 = average value})")
cat(sql) or something that you can execude in the playground: [{id=1, value=2},
{id=2, value=3},
{id=3, value=4},
{id=6, value=2},
{id=5, value=3},
{id=4, value=4},]
window rolling:3 (
sort id
derive {sma3 = average value}
) which produces WITH table_0 AS (
...
)
SELECT
id,
value,
AVG(value) OVER (
ORDER BY
id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) AS sma3
FROM
table_0
ORDER BY
id
-- Generated by PRQL compiler version:0.9.5 (https://prql-lang.org) |
Thanks for reply, that make sense, could be mentioned in documentation. |
I agree we could elide the (Thanks for the excellent reply @snth!) |
I reopened the issue because that Putting a PRQL Input [{id=1, value=2},
{id=2, value=3},
{id=3, value=4},
{id=6, value=2},
{id=5, value=3},
{id=4, value=4},]
sort value
window rolling:3 (
sort id
derive {sma3 = average value}
) SQL Output WITH table_0 AS (
SELECT
1 AS id,
2 AS value
UNION
ALL
SELECT
2 AS id,
3 AS value
UNION
ALL
SELECT
3 AS id,
4 AS value
UNION
ALL
SELECT
6 AS id,
2 AS value
UNION
ALL
SELECT
5 AS id,
3 AS value
UNION
ALL
SELECT
4 AS id,
4 AS value
)
SELECT
id,
value,
AVG(value) OVER (
ORDER BY
id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) AS sma3
FROM
table_0
ORDER BY
id
-- Generated by PRQL compiler version:0.9.5 (https://prql-lang.org) Expected SQL Output WITH table_0 AS (
SELECT
1 AS id,
2 AS value
UNION
ALL
SELECT
2 AS id,
3 AS value
UNION
ALL
SELECT
3 AS id,
4 AS value
UNION
ALL
SELECT
6 AS id,
2 AS value
UNION
ALL
SELECT
5 AS id,
3 AS value
UNION
ALL
SELECT
4 AS id,
4 AS value
)
SELECT
id,
value,
AVG(value) OVER (
ORDER BY
id ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) AS sma3
FROM
table_0
ORDER BY
value
-- Generated by PRQL compiler version:0.9.5 (https://prql-lang.org) |
(for anyone comparing the queries — it's the final term that is incorrect — |
Agree, it is a bug. Documentation for presenting use of local sort was already resolved in #3634 |
What's up?
Having the following data
d
How can I generate following SQL
According to documentation it seems that I have to sort whole dataset rather than an input to windowing function, therefore query that is being generated by prql compiler includes an extra unwanted
ORDER BY id
at the end.How can I define sorting locally for each window function call locally? so I don't have to clutter my SQL query with unwanted clauses.
The text was updated successfully, but these errors were encountered: