在 SQL 查询中选择最近的行
Selecting most recent rows in a SQL query
我想连接两个 table,为 table 1 中存在的 ID 值选择最近的行。
即对于 table 1 中的每个 ID 值,只有 return 最近添加的行有一个 ID 值。
例如,table 1 看起来像这样:
Columns: ID-value, date-added, other-information
row 1: ID_1, 21/2/2020-12:30, other_newer_information...
row 2: ID_1, 21/2/1990-12:30, other_older_information...
因此,如果在此 table 中两次找到相同的 ID 值,则只有 return 较新的条目,在上述情况下的第 1 行。
然后我想将这些行与第二个 table 中存在的信息连接起来。
例如table 2 看起来像这样:
Columns: column-present-in-table-1, another-column-present-in-table-1, other-columns
row 1: some_data, some_more_data... additional data
row 2:- some_data, infor_2: some_more_data... additional data
etc
- 我的 sql 下面的查询按预期工作,用于连接两个 tables
- 但我无法解决的是,当在多个日期输入了重复的 ID 值时,如何仅 return 来自 table 1 的最新行
- 也不确定日期过滤是否应该作为
SELECT
的一部分发生,或者当第一次从 table 1 获取数据时发生
从 Whosebug 的其他地方来看,建议类似于 MAX(date_time)
- 但我的理解是这只会 return 最大日期时间值,而不是最近的行 - 如果我错了
我的查询看起来像这样:
SELECT
id_1,
info_1,
info_2,
date_time,
info_3,
info_4,
max(info_3),
min(info_4)
FROM table_1
INNER JOIN table_2
ON table_1.info_1 = table_2.infor_1
AND table_1.info_2 = table_2.infor_2
WHERE id_1 in ("id1", "id2")
AND info_3 = "10"
GROUP BY id_1, info_1, info_2, info_3, info_4
ORDER BY id_1, id_2, date_time DESC
关于 Whosebug 的其他建议:SELECT TOP id_1...min(info_4)
(给出语法错误),ORDER BY id_1... date_time DESC LIMIT 1
(只有 return 一行 - 即最近的日期时间)。
ROW_NUMBER() OVER (PARTITION BY id, ORDER BY date_time) AS 'row_number'
return是行号,不是最近的行。
So if the same ID value is found twice in my table, only return the more recent entry, row 1 in the above case.
您可以使用 row_number()
:
select t.*
from (select t.*,
row_number() over (partition by id order by date_time desc) as seqnum
from mytable t
) t
where seqnum = 1;
我真的不知道你的查询与你的问题有什么关系。如果您的“table”确实是查询的结果,那么只需使用 CTE 或子查询:
with t as (
<your query here>
)
<query with row_number here>
我想连接两个 table,为 table 1 中存在的 ID 值选择最近的行。
即对于 table 1 中的每个 ID 值,只有 return 最近添加的行有一个 ID 值。 例如,table 1 看起来像这样:
Columns: ID-value, date-added, other-information
row 1: ID_1, 21/2/2020-12:30, other_newer_information...
row 2: ID_1, 21/2/1990-12:30, other_older_information...
因此,如果在此 table 中两次找到相同的 ID 值,则只有 return 较新的条目,在上述情况下的第 1 行。
然后我想将这些行与第二个 table 中存在的信息连接起来。 例如table 2 看起来像这样:
Columns: column-present-in-table-1, another-column-present-in-table-1, other-columns
row 1: some_data, some_more_data... additional data
row 2:- some_data, infor_2: some_more_data... additional data
etc
- 我的 sql 下面的查询按预期工作,用于连接两个 tables
- 但我无法解决的是,当在多个日期输入了重复的 ID 值时,如何仅 return 来自 table 1 的最新行
- 也不确定日期过滤是否应该作为
SELECT
的一部分发生,或者当第一次从 table 1 获取数据时发生
从 Whosebug 的其他地方来看,建议类似于 MAX(date_time)
- 但我的理解是这只会 return 最大日期时间值,而不是最近的行 - 如果我错了
我的查询看起来像这样:
SELECT
id_1,
info_1,
info_2,
date_time,
info_3,
info_4,
max(info_3),
min(info_4)
FROM table_1
INNER JOIN table_2
ON table_1.info_1 = table_2.infor_1
AND table_1.info_2 = table_2.infor_2
WHERE id_1 in ("id1", "id2")
AND info_3 = "10"
GROUP BY id_1, info_1, info_2, info_3, info_4
ORDER BY id_1, id_2, date_time DESC
关于 Whosebug 的其他建议:SELECT TOP id_1...min(info_4)
(给出语法错误),ORDER BY id_1... date_time DESC LIMIT 1
(只有 return 一行 - 即最近的日期时间)。
ROW_NUMBER() OVER (PARTITION BY id, ORDER BY date_time) AS 'row_number'
return是行号,不是最近的行。
So if the same ID value is found twice in my table, only return the more recent entry, row 1 in the above case.
您可以使用 row_number()
:
select t.*
from (select t.*,
row_number() over (partition by id order by date_time desc) as seqnum
from mytable t
) t
where seqnum = 1;
我真的不知道你的查询与你的问题有什么关系。如果您的“table”确实是查询的结果,那么只需使用 CTE 或子查询:
with t as (
<your query here>
)
<query with row_number here>