I have the following table:
CREATE TABLE "posting" (
"id" integer NOT NULL PRIMARY KEY,
"amount" real NOT NULL,
"balance" real NOT NULL,
"account_id" integer NOT NULL REFERENCES "account" ("id"),
"asset_type_id" integer NOT NULL REFERENCES "asset_type" ("id")
)
For this table, I manually generate ids in a way that there's no gaps(records can't be deleted). It is guaranteed that the following statement will return the latest record :
SELECT * FROM posting ORDER BY id DESC LIMIT 1
The problem is that now I need to retrieve not only the last, but the last record for each combination of 'account_id' and 'asset_type_id'. For example, lets say I have two 'accounts' and two 'asset_types' (both with ids 1 and 2) and the following records(omiting amount and balance):
id | account_id | asset_type_id
1 | 1 | 1
2 | 2 | 1
3 | 1 | 2
4 | 2 | 1
5 | 2 | 2
6 | 2 | 2
It will return me records 6, 4, 3 and 1 since records 5 and 2 were "replaced" by 6 and 4 respectively. I have no idea how to represent this in SQL, any help is appreciated.