简体   繁体   English

查询查找 sql 中的最新记录

[英]Query for finding latest record in sql

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 ID baseCur基线 Curr电流 rate速度 date日期
1 1个 INR印度卢比 USD美元 81 81 2022-11-09 2022-11-09
2 2个 INR印度卢比 USD美元 82 82 2022-11-08 2022-11-08
3 3个 INR印度卢比 USD美元 80 80 2022-11-06 2022-11-06
4 4个 INR印度卢比 CAD计算机辅助设计 56 56 2022-11-05 2022-11-05
5 5个 INR印度卢比 RUB .74 .74 2022-11-04 2022-11-04
6 6个 INR印度卢比 CAD计算机辅助设计 57 57 2022-11-12 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我需要找到所有小于 2022-11-09 的最新货币汇率。在任何给定日期,只有任何特定货币的汇率

so expected output所以预计 output

id ID baseCur基线 Curr电流 rate速度 date日期
2 2个 INR印度卢比 USD美元 82 82 2022-11-08 2022-11-08
4 4个 INR印度卢比 CAD计算机辅助设计 56 56 2022-11-05 2022-11-05
5 5个 INR印度卢比 RUB .74 .74 2022-11-04 2022-11-04

Explanantion of output: output说明:

Id 1,6 rejected: cause they are greater than 2022-11-09 date Id 1,6 被拒绝:因为它们大于 2022-11-09 日期

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 Id 3 被拒绝,因为我们在第 2 行中还有一条 INR 到 CAD 的记录,而且它的日期对 Id 3 来说更新

You can use a window function such as DENSE_RANK() if DB version is 8.0+ in order to determine the latest records by using the query below如果数据库版本为8.0+ ,则可以使用 window function 等DENSE_RANK()以便使用以下查询确定最新记录

WITH t AS
(
 SELECT t.*, DENSE_RANK() OVER (PARTITION BY baseCur, Curr ORDER BY date DESC) AS dr
   FROM t
  WHERE date < '2022-11-09' 
)
SELECT id, baseCur, Curr, rate, date
  FROM t
 WHERE dr = 1

But, 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.除了为此使用 window function 的选项之外,您还可以使用子查询。 In the subquery, you will catch every currency with the latest date:在子查询中,您将捕获具有最新日期的每种货币:

SELECT 
curr, MAX(yourdate) maxDate
FROM yourtable
WHERE yourdate < '2022-11-09' 
GROUP BY curr; 

This query will produce this outcome:此查询将产生以下结果:

Curr电流 maxDate最大日期
2 2个 2022-11-08 2022-11-08
4 4个 2022-11-05 2022-11-05
5 5个 2022-11-04 2022-11-04

This result can be used by applying a JOIN clause or IN clause from a main query.可以通过从主查询应用JOIN子句或IN子句来使用此结果。

This will add the other columns.这将添加其他列。

SELECT y.id, y.baseCur, y.curr, y.rate, y.yourdate
FROM yourtable y 
JOIN (SELECT 
curr, MAX(yourdate) maxDate
FROM yourtable
WHERE yourdate < '2022-11-09' 
GROUP BY curr) maxDates
ON y.curr = maxDates.curr 
AND y.yourdate = maxDates.maxDate
ORDER BY id;

Thus, the complete intended result will be created:因此,将创建完整的预期结果:

id ID baseCur基线 Curr电流 rate速度 date日期
2 2个 INR印度卢比 USD美元 82 82 2022-11-08 2022-11-08
4 4个 INR印度卢比 CAD计算机辅助设计 56 56 2022-11-05 2022-11-05
5 5个 INR印度卢比 RUB .74 .74 2022-11-04 2022-11-04

To point that out: I think using a window function should be prefered if possible.要指出这一点:我认为如果可能的话,应该首选使用 window function。

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.您可以使用子查询获取所需的 output,如下所示,它从每种货币获取最新记录。

-- 1. Based on id column
SELECT * FROM sometable as t WHERE t.id = 
(SELECT MAX(id) FROM sometable WHERE Curr = t.Curr and date < '2022-11-09');
-- 2. Based on date column
SELECT * FROM sometable as t WHERE t.date = 
(SELECT MAX(date) FROM sometable WHERE Curr = t.Curr and date < '2022-11-09');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM