简体   繁体   English

如何使用前 1 在子查询中获取两列?

[英]How can I get two columns in a subquery using top 1?

Here's the thing.事情就是这样。 I have these two tables:我有这两张表:

table A:表一:

id    col1    date_x
A     xxxx    2020-02-02
B     yyyy    2020-02-02
C     zzzz    2020-02-02

table B表 B

id    col2    date_y
A     yyyy    2020-01-02
A     yyyy    2020-02-02
A     yyyy    2020-03-02

I wanted to bring col2 when date_y is the highest possible but it has to be lower than date_x.我想在 date_y 可能最高但必须低于 date_x 时带上 col2。

This is what I've done:这就是我所做的:

select *,
       (
         select top 1 col2
         from table_B
         where table_B.date_y < a.date_x 
         and table_B.id = a.id
       ) as col2                   
from table_A a

Now, I wanted to bring date_y as well, in order to do some validation.现在,我也想带上 date_y,以便进行一些验证。

What is the best way of doing this?这样做的最佳方法是什么? I thought about creating another (select top 1...) but this seems very inefficient.我考虑过创建另一个(选择前 1 个...),但这似乎效率很低。 Another join would also be inefficient.另一个联接也将是低效的。

You can join the tables on your conditions and use MAX() and FIRST_VALUE() window functions to get the date_y and col2 values:您可以根据您的条件加入表格并使用MAX()FIRST_VALUE() window 函数来获取date_ycol2值:

select distinct a.*,
       first_value(b.col2) over (partition by a.id order by b.date_y desc, b.col2) col2,
       max(b.date_y) over (partition by a.id) date_y
from tableA a left join tableB b
on b.id = a.id and b.date_y < a.date_x

You may change the LEFT join to an INNER join if you want only matched rows from the 2 tables.如果您只想要两个表中的匹配行,您可以将LEFT连接更改为INNER连接。
See the demo .请参阅演示

Your approach using a correlated subquery is OK - and Redshift supports top (although I prefer limit , that is more widely supported in other databases).您使用相关子查询的方法是可以的 - Redshift 支持top (虽然我更喜欢limit ,这在其他数据库中得到更广泛的支持)。

However you are missing an order by clause in the subquery - without it, you get an unpredictable row out of those that satisfy the where clause, which is not what you want.但是,您在子查询中缺少order by子句 - 没有它,您会从满足where子句的行中得到不可预测的行,这不是您想要的。

I would recommend:我会推荐:

select 
    a.*,
    (
        select col2
        from table_B b
        where b.date_y < a.date_x and b.id = a.id
        order by b.date_y desc
        limit 1
    ) as col2                   
 from table_A a

For performance, consider an index on table_B(id, date_y, col2) .为了提高性能,请考虑table_B(id, date_y, col2)上的索引。

暂无
暂无

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

相关问题 在 Django 中,如何根据子查询的子查询获取记录数? - In Django, how can I get a count of records based on a subquery of a subquery? 当我尝试使用 `and` 从表(子查询的表)中获取两列时,出现错误“AND 的参数必须是 Boolean 类型,而不是整数类型” - Getting an error 'argument of AND must be type Boolean, not type integer' when I try to get two columns from a table(table from subquery) using `and` 给定一组 boolean 列,我如何获得具有最多真值的前 5 列 - Given a set of boolean columns how can I get the top 5 columns with the most amount of true values 如何使用子查询更新表 - How can I update a table by using subquery 我有两个选择查询结合使用UNION。 我在一列中得到两个结果如何在两列中得到它 - I have two select queries combined using UNION. I am getting two results in one column how can I get it in two columns 如何使用通配符加入两个表列? - How can I JOIN two tables columns using a wildcard? 如何将运算符IN与返回两列的子查询一起使用 - How use the operator IN with a subquery that returns two columns 我如何结合两列来过滤在两者之间使用? - How can i combine two columns to filter on using between? 如何计算前两个标签? - How can I calculate top two tags? 如何在子查询中获取结果值,并将其用于使用MySQL进行比较? - How can I get the result value in a subquery and use it for comparison using MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM