简体   繁体   中英

Use MAX() of a date in the Google Sheets query function, to get the value of another column

I have a table like this:

id  descripcion precio  fecha
1   gomitas     5       1/2/2020
1   gomitas     2       2/3/2020
2   DRF         56      2/3/2020
3   BULLDOG     8       2/3/2020
1   gomitas     10      1/3/2020
3   BULLDOG     9       1/4/2020
2   DRF         7       1/4/2020

And this is the desired result:

id  precio  fecha
1   2       2/3/2020
2   7       1/4/2020
3   9       1/4/2020

That is, group by product (its id), with the maximum date, giving the price detail. In short, to give me the last price of a certain product.

I tried this:

=QUERY(A:D,"SELECT A,C, MAX(D) GROUP BY (A)")

But it asks me to group C (price column). Which doesn't work for me. I found this too:

=query(A:D,"SELECT A,C,D ORDER BY D DESC LIMIT 3") 

But it doesn't work for me, because the limit is variable.

try:

=SORTN(SORT({A:A, C:D}, 3, 0), 9^9, 2, 1, 1)

0


a simple query can do only:

=QUERY(A:D, "select A,max(D) where A is not null group by A label max(D)''")

or

=QUERY(A:D, "select A,C,max(D) where A is not null group by A,C label max(D)''")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM