简体   繁体   English

你如何连接表,以便右表值取决于左表中的两个不同行

[英]How do you join tables so that right table values depend on two different rows in left table

Sorry for messy title, if you are a moderator and know a better title, feel free to change.抱歉标题凌乱,如果您是版主并且知道更好的标题,请随时更改。

Say, we have two SQL tables说,我们有两个 SQL 表

intervals           vals       
--------            -------    
since               val        
--------            -------    
 1                  1          
 4                  2          
 8                  3
 20                 4          
 ...                ...
 500                100

I want to make a join so that "since" field from intervals table would be a lower bound for a "val".我想进行连接,以便间隔表中的“since”字段成为“val”的下限。 And "since" values that have no "val" that is larger would not appear.并且不会出现没有更大“val”的“since”值。 See what I want to get:看看我想得到什么:

since val
--------------
1     1
1     2
1     3
4     4
4     5
4     6
4     7
8     8
8     9
.....

How do I do it in generic SQL?如何在通用 SQL 中做到这一点? Postgres-only solution will fit as well.仅限 Postgres 的解决方案也适用。

Rather than think of it as "multiple rows", think of it as a range .与其将其视为“多行”,不如将其视为一个范围

This does what you want:这可以满足您的要求:

select i.since, v.val
from intervals i
join vals v on v.val between i.since and 
    (select min(since) - 1 from intervals where since > i.since)
order by 1, 2;

Test code (run on postgres as per OP's question):测试代码(根据 OP 的问题在 postgres 上运行):

create table intervals (since int);
create table vals (val int);
insert into intervals values (1), (4), (8), (20), (500);
insert into vals values (1), (2), (3), (4), (5), (6), (7), (8), (9), (100);

Output from above query: Output 来自上述查询:

1   1
1   2
1   3
4   4
4   5
4   6
4   7
8   8
8   9
20  100

Credit to RhodiumToad on #postgresql归功于 #postgresql 上的 RhodiumToad

SELECT  * 
FROM    vals v 
JOIN    (select since
              , lead(since) over (order by since) as "end" 
         from intervals) s 
         ON ( v.val >= s.since 
             AND ((v.val >= s."end") IS NOT TRUE)
            )
;

暂无
暂无

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

相关问题 SQL Server:联接两个表,其中左表包含其每个标识符中的右表的所有行 - SQL Server: join two tables where left table contains all rows of right table in each of its identifier 您如何在同一查询中从2个不同的表中多个左联接同一表? - How do you multiple left join the same table from 2 different tables in the same query? 如何在不对表中的不同值重复相同的行的情况下联接两个表 - How to join two tables without repeating the same rows for different values in table 使用左外连接连接两个表并根据右表列填充新列值 - Joining two tables using left outer join and population a new column values based on the right table column 尝试在两个表上进行左联接,但只希望右表中的一个 - Trying to do a LEFT JOIN on two tables but want only one from the RIGHT TABLE 在SQL连接操作中,如何从左侧联接中获取行,以及如何从右侧表中获取两列的汇总 - In an SQL join operation, how to get the rows from the left join and only the aggregate of two columns from the right table 如何通过从左表中获取所有行并仅在右表中匹配来连接 2 个表 - How to join 2 tables with getting all rows from left table and only matching ones in right 左联接两个表,然后仅将新行追加到表 - Left join two tables, and then append only new rows to table 在左连接中选择取决于另一个表中字段总和的行? - Select rows in left join which depend on sum of a field in the other table? 如何在右表中的特定行上进行左连接? - How to make left join on specific rows in right table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM