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_value does not return the last value #17125

Open
laurynas-pliuskys opened this issue Dec 20, 2021 · 2 comments
Open

last_value does not return the last value #17125

laurynas-pliuskys opened this issue Dec 20, 2021 · 2 comments

Comments

@laurynas-pliuskys
Copy link

Briefly, the function last_value() does not return the last value in a window. Only running first_value() with ORDER BY ... DESC returns the last value correctly.

The following is my test data:

c_caseid | country | city  | AssignDate |
1c741bd70b2322007518478d83673af3 | USA | Kansas City  | 1481560249000 |  
1c741bd70b2322007518478d83673af3 | USA | Solana Beach | 1481590617000 |

When I run the following query

    SELECT DISTINCT
        c_caseid,
        first_value(city) OVER (PARTITION BY c_caseid ORDER BY AssignDate) AS IncidentFirstAssignedToCity,
        last_value(city) OVER (PARTITION BY c_caseid ORDER BY AssignDate) AS IncidentLastAssignedToCity
    FROM test

I get the following result:

c_caseid | IncidentFirstAssignedToCity | IncidentLastAssignedToCity |  
1c741bd70b2322007518478d83673af3 | Kansas City | Kansas City |  
1c741bd70b2322007518478d83673af3 | Kansas City | Solana Beach | 

It's as if last_value() provides two different results, instead of one? When I change the query to first_value() and order in reverse, as follows:

    SELECT DISTINCT
        c_caseid,
        first_value(city) OVER (PARTITION BY c_caseid ORDER BY AssignDate) AS IncidentFirstAssignedToCity,
        first_value(city) OVER (PARTITION BY c_caseid ORDER BY AssignDate DESC) AS IncidentLastAssignedToCity
    FROM test

Only then I get the correct result:

c_caseid | IncidentFirstAssignedToCity | IncidentLastAssignedToCity |  
1c741bd70b2322007518478d83673af3 | Kansas City | Solana Beach | 

Am I doing something wrong, or is this actually a bug? I'm running the queries on AWS Athena.

@Guazhen
Copy link

Guazhen commented Dec 21, 2021

``
last_ Value: the default statistical range is rows between unbounded preceding and current row

example:
SELECT DISTINCT
c_caseid,
first_value(city) OVER (PARTITION BY c_caseid ORDER BY AssignDate) AS IncidentFirstAssignedToCity,
last_value(city) OVER (PARTITION BY c_caseid ORDER BY AssignDate rows between unbounded preceding and current row ) AS IncidentLastAssignedToCity
FROM test

@laurynas-pliuskys
Copy link
Author

laurynas-pliuskys commented Dec 21, 2021

I see, yes I can fix it by entering

last_value(city) OVER (PARTITION BY c_caseid ORDER BY AssignDate ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

But then either the implementation of the function, or documentation need to be adjusted. It seems counter-intuitive that I need to specify which rows to use when the documentation clearly says it considers the whole window; and considering I am not defining any rows, offsets, etc. I assume the entire partition is considered as a window?

What is the default of first_value()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants