Given a currency table I need to find the latest record of conversion rate which is less than given particular date
Input table structure given below:
id | baseCur | Curr | rate | date |
---|---|---|---|---|
1 | INR | USD | 81 | 2022-11-09 |
2 | INR | USD | 82 | 2022-11-08 |
3 | INR | USD | 80 | 2022-11-06 |
4 | INR | CAD | 56 | 2022-11-05 |
5 | INR | RUB | .74 | 2022-11-04 |
6 | INR | CAD | 57 | 2022-11-12 |
Problem statement:
I need to find all latest currencies rate that is less than 2022-11-09.On any given date there will be only conversation rate for any particular currency
so expected output
id | baseCur | Curr | rate | date |
---|---|---|---|---|
2 | INR | USD | 82 | 2022-11-08 |
4 | INR | CAD | 56 | 2022-11-05 |
5 | INR | RUB | .74 | 2022-11-04 |
Explanantion of output :
Id 1,6 rejected : cause they are greater than 2022-11-09 date
Id 3 rejected cause we have one more record for INR to CAD in row 2 and its date is more new to Id 3
3
Answers
You can use a window function such as
DENSE_RANK()
if DB version is8.0+
in order to determine the latest records by using the query belowBut, notice that this query returns the ties(equal date values) as well if there is any.
Demo
Beside the option to use a window function for that, you could also use a subquery. In the subquery, you will catch every currency with the latest date:
This query will produce this outcome:
This result can be used by applying a
JOIN
clause orIN
clause from a main query.This will add the other columns.
Thus, the complete intended result will be created:
To point that out: I think using a window function should be prefered if possible.
They just have the "disadvantage" older DB’s don’t provide them and they often differ depending on the DB type.
So, if a query is required that works always on each DB type and DB version, this way of using a subquery becomes helpful.
You can fetch the desired output using subquery, as shown below, which fetches latest record from each currency.