使用范围内的日期来加入和显示所有日期?

Using the dates in ranges to join and display all dates?

我有两个 table,table A 和 Table B。

Table A 包括人们的发薪时间和日历日期。 Table B 有开始日期、结束日期和支付期信息。

示例数据来自 table A:

Calendar_Date   PayHours   Name
11/23/2020      10.0       Scott, Michael
12/02/2020      12.0       Harper, Jim
12/03/2020      12.0       Kim, Maggie
12/10/2020      24.0       Rogers, Steve

示例数据来自 table B:

Start_date  End_Date    PayPeriod
2020-11-22  2020-12-05  2020-12-wk1
2020-12-06  2020-12-19  2020-12-wk3

我需要的是加入这些table,这样我就能得到这样的结果。

Calendar_Date   PayHours   Name             PayPeriod
11/23/2020      10.0       Scott, Michael   2020-12-wk1
12/02/2020      12.0       Harper, Jim      2020-12-wk1
12/03/2020      12.0       Kim, Maggie      2020-12-wk1
12/10/2020      24.0       Rogers, Steve    2020-12-wk3

显然加入 A.Calendar_date = B.Start_date(或 B.end_date)不会给我所需的结果。但是有没有办法将这两个 table 与日期连接起来?

您可以在属于支付周期范围的日历日期加入,如下所示:

select a.*, b.payperiod
from tablea a
inner join tableb b 
    on  c.calendar_date >= b.start_date
    and c.calendar_date <= b.end_date