I've a simple bigquery table with 3 columns (and some example data) below:
|---------------------|------------------|------------|
| Name | Time | Value |
|---------------------|------------------|------------|
| a | 1 | x |
|---------------------|------------------|------------|
| a | 2 | y |
|---------------------|------------------|------------|
| a | 3 | z |
|---------------------|------------------|------------|
| b | 1 | x |
|---------------------|------------------|------------|
| b | 4 | y |
|---------------------|------------------|------------|
For each name, I'd like to return the value with the max time.
For the above table the 3rd and 5th row should be returned, e.g.,
|---------------------|------------------|------------|
| Name | Time | Value |
|---------------------|------------------|------------|
| a | 3 | z |
|---------------------|------------------|------------|
| b | 4 | y |
|---------------------|------------------|------------|
It is roughly like: (1) first group by Name, (2) find out the max time in each group, (3) identify the row with the max time.
Seems for (1) and (2), we can use group by + max(), but i'm not sure how to achieve the (3) step.
Anyone has ideas of what's the best query I can write to achieve this purpose.
Thanks a lot.
mark accepted answer
by using the tick on the left of the posted answer, below the voting. See meta.stackexchange.com/questions/5234/… for why it is important! Also important tovote on answer
. Vote up answers that are helpful. ... You can check about what to do when someone answers your question - stackoverflow.com/help/someone-answers. Following these simple rules you increase your own reputation score and at the same time you keep us motivated to answer your questions :o) please consider! - Mikhail Berlyant