使用 start_end 和 end_date 检索特定月份的所有记录

Retrieve all Records for a particular Month using start_end and end_date

我有一个 table 具有以下结构和数据

Product           startdate         enddate 
ProdA             01/01/2020        15/01/2020
ProdB             29/01/2020        02/02/2020
ProdC             02/02/2020        03/03/2020
ProdD             20/12/2019        20/12/2020

我需要一个 sql 的 redshift 查询,returns 我提供了我提供的一个月内有效的所有产品。如果该产品在开始日期和结束日期之间有该月的任何一天,则考虑该月的有效性

所以如果我传递日期 01/01/2020(例如,如果您想将其格式化为 YYYY-mm,可以根据解决方案进行修改。)我正在寻找在 2020 年 1 月有效的记录并希望返回以下产品

ProdA 生产数据集 产品

你可以使用这个逻辑:

where startdate <= last_day('2020-01-01') and
      enddate >= '2020-01-01'

也就是说,如果开始日期在月底之前,而结束日期在月初或之后,则存在重叠。