Skip to content
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

last / first / nth not working with PostgreSQL #531

Closed
florianWickler opened this issue Aug 11, 2014 · 3 comments
Closed

last / first / nth not working with PostgreSQL #531

florianWickler opened this issue Aug 11, 2014 · 3 comments
Assignees
Labels
Milestone

Comments

@florianWickler
Copy link

@florianWickler florianWickler commented Aug 11, 2014

Please have a look at
http://stackoverflow.com/questions/25184965/r-dplyr-how-to-use-last-first-nth-correct-in-a-sql-database

 library("dplyr")
 mytab <- tbl(src_postgres(dbname, host, port, user, password), "catalog")
 mytab <- group_by(mytab, catalog_id)
 mytab<- mutate(mytab, curr2 = last(currency))
 mytab

RS-DBI driver: (could not Retrieve the result : ERROR: function first_value() does not exist

mytab$query

SELECT "catalog_id", "currency", "skonto", FIRST_VALUE() OVER (PARTITION BY "catalog_id") AS "curr2" FROM "catalog"

In my opinion the query should be:

SELECT "catalog_id", "currency", "skonto", FIRST_VALUE(currency) OVER (PARTITION BY "catalog_id") AS "curr2" FROM "catalog"

@hadley hadley added the bug label Aug 26, 2014
@hadley hadley added this to the 0.3 milestone Aug 26, 2014
@hadley hadley self-assigned this Aug 26, 2014
@hadley
Copy link
Member

@hadley hadley commented Aug 26, 2014

What version of postgresql do you have? Looks like it's available from at least 9.0 on. http://www.postgresql.org/docs/9.0/static/functions-window.html.

Could you please provide a reproducible example?

@florianWickler
Copy link
Author

@florianWickler florianWickler commented Aug 27, 2014

I'm using PostgreSQL 9.3.2.
In my opinion the dplyrs' SQL translator is wrong because the PostgreSQL-DB expects a value argument for the statement first_value etc. But you don't assign it:
FIRST_VALUE(currency) instead of FIRST_VALUE().

I don' t know how to provide a reproducible example at the moment.

@hadley
Copy link
Member

@hadley hadley commented Aug 28, 2014

Simple reproducible example:

mtcars2 <- copy_to(src_postgres(), mtcars)
mtcars2 %>%
  group_by(cyl) %>% 
  mutate(mpg2 = last(mpg))

@hadley hadley closed this in f633484 Aug 28, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jun 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants