0

How can I group by (property_id) and extract the data (travel_date) for the entry with the minimum value (total_price) per group?

I'm using MS SQL Server, but happy to convert from any other SQL-RDBMS example.

Database table looks like:

trip_id, property_id, travel_date, total_price
1, 1, 2023-04-01, 1000
2, 1, 2023-04-02, 1100
3, 1, 2023-04-03, 1200
4, 2, 2023-04-01, 1000
5, 2, 2023-04-02, 900
6, 2, 2023-04-03, 2200

Desired result looks like:

trip_id, property_id, travel_date, MIN(total_price)
1, 1, 2023-04-01, 1000
5, 2, 2023-03-02, 900

I have a ton of WHERE clauses in the real query, so ideally looking to avoid a subquery which would need to duplicate all the WHERE's.

Thom A
  • 88,727
  • 11
  • 45
  • 75
user72003
  • 425
  • 1
  • 5
  • 7

0 Answers0