Return rows with maximum value of a column

František Škandera

I have a table that looks something like this:

date       | product | price
--------------------------------
17/01/2015 | milk    | 2.54
18/01/2015 | milk    | 2.47
23/01/2015 | milk    | 2.61
21/01/2015 | eggs    | 1.35
04/02/2015 | eggs    | 1.36
27/01/2015 | eggs    | 1.31

What I need is a select that returns me the latest price of each product, that is the one with the maximum date. Desired result here would be:

23/01/2015 | milk    | 2.61
04/02/2015 | eggs    | 1.36

I tried this:

select max(date), product, price FROM table GROUP BY product, price

but it didn't work as expected.

jarlh

Use a correlated subquery to find each product's last date:

select date, product, price
from table t1
where date = (select max(date) from table t2
              where t1.product = t2.product)

(After reading Damien_The_Unbeliever's comment I want to add that if several entries exist with same max date for a product, they will all be returned.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to return multiple rows based on maximum value in one of its column in SQL

From Dev

How to select rows that have a maximum value for a column

From Dev

GroupBy column and filter rows with maximum value in Pyspark

From Dev

How Select The Rows In A Dataframe with the Maximum Value in a Column

From Dev

Finding maximum value in a column and return row number

From Dev

Return value in column based on value of two rows?

From Dev

Return rows with duplicate value in column if one of the rows contains specific value

From Dev

Return Multiple Rows from Column Value

From Dev

Return the maximum value of rows in a 2d array

From Dev

Extract the unique rows with maximum value in another column in R dataframe

From Dev

How to select all rows that share a maximum value in a column using SQL

From Dev

SQL Query: Get rows where value of certain column is maximum

From Dev

Return column names based on which holds the maximum value in the record

From Dev

Query to return rows in date range but return only max value of column

From Dev

Return multiple rows grouped by a column and named by other column value

From Dev

Return multiple rows grouped by a column and named by other column value

From Dev

Return the index for the maximum value

From Dev

Maximum value in a column

From Dev

Maximum value in a gridview column

From Dev

Maximum value in a gridview column

From Dev

Maximum value in column of a matrix

From Dev

How to get rows from numpy 2d where column value is maximum from group by other column?

From Dev

return max value from panda dataframe as a whole, not based on column or rows

From Dev

TSQL return distinct rows based on the preferred value of a column

From Dev

SQL Query to return rows where a column value appears multiple time

From Dev

MySQL return all rows where a value exists in a column

From Dev

sql query to return all rows with same column value

From Dev

Return the column value that common among multiple rows meeting criteria

From Dev

Excel to return matched value in rows from a column array

Related Related

  1. 1

    How to return multiple rows based on maximum value in one of its column in SQL

  2. 2

    How to select rows that have a maximum value for a column

  3. 3

    GroupBy column and filter rows with maximum value in Pyspark

  4. 4

    How Select The Rows In A Dataframe with the Maximum Value in a Column

  5. 5

    Finding maximum value in a column and return row number

  6. 6

    Return value in column based on value of two rows?

  7. 7

    Return rows with duplicate value in column if one of the rows contains specific value

  8. 8

    Return Multiple Rows from Column Value

  9. 9

    Return the maximum value of rows in a 2d array

  10. 10

    Extract the unique rows with maximum value in another column in R dataframe

  11. 11

    How to select all rows that share a maximum value in a column using SQL

  12. 12

    SQL Query: Get rows where value of certain column is maximum

  13. 13

    Return column names based on which holds the maximum value in the record

  14. 14

    Query to return rows in date range but return only max value of column

  15. 15

    Return multiple rows grouped by a column and named by other column value

  16. 16

    Return multiple rows grouped by a column and named by other column value

  17. 17

    Return the index for the maximum value

  18. 18

    Maximum value in a column

  19. 19

    Maximum value in a gridview column

  20. 20

    Maximum value in a gridview column

  21. 21

    Maximum value in column of a matrix

  22. 22

    How to get rows from numpy 2d where column value is maximum from group by other column?

  23. 23

    return max value from panda dataframe as a whole, not based on column or rows

  24. 24

    TSQL return distinct rows based on the preferred value of a column

  25. 25

    SQL Query to return rows where a column value appears multiple time

  26. 26

    MySQL return all rows where a value exists in a column

  27. 27

    sql query to return all rows with same column value

  28. 28

    Return the column value that common among multiple rows meeting criteria

  29. 29

    Excel to return matched value in rows from a column array

HotTag

Archive