0

I have a data table like the following example. I want to get the Sum of the column 'price'. The problem is, that i have the same data ('id') multiple times, which is updated from the program multiple times, there i only want the newest data entry.

f.e. the data

id price updatedate
1 **** 20 11.07.2023
1 25 08.07.2023
1 85 06.07.2023
2 70 10.07.2023
2 **** 15 11.07.2023
3 50 01.07.2023
3 **** 90 11.07.2023
3 60 07.07.2023

So in this example, i want to sum the **** rows, because this are the newest data entrys for every id

I'm trying to search an SQL Function to get the sum of these values (price column), which is here 20 + 15 + 90 = 125

If i try GROUP BY, the result is given in three rows for every id und sums up every data row with this id.

SELECT SUM (price) FROM table GROUP BY id 

but that isn't working....

nbk
  • 45,398
  • 8
  • 30
  • 47
Fabian S
  • 1
  • 1
  • 1
    Use a CTE for the `GROUP BY id` with `MAX` to get the latest `updatedate`, then `JOIN` that CTE with the source table to get the `price` for the matching `id + updatedate` row. – Dai Jul 11 '23 at 20:27
  • Thanks Dai! Have you an example for the SQL Syntax here? – Fabian S Jul 11 '23 at 21:08
  • See the linked question that this has been closed as a duplicate-of. – Dai Jul 11 '23 at 21:09

0 Answers0