【发布时间】:2021-06-28 20:48:33
【问题描述】:
我在一段时间内获得了一些数据,但在弄清楚一些细节时遇到了困难。
例如,我有大量具有活动START 和STOP 日期的数据行。如果出现以下三种情况之一,我已经执行了笛卡尔积将我的特定数据行加入到该特定月份:
- 项目活动开始日期出现在该特定月份的第一天之前,而该项目的活动停止日期出现在该月的第一天之后。
(ITEM_START<= CalendarMonthStart and ITEM_STOP>= CalendarMonthStart) - 项目活动开始日期出现在该特定月份的第一天之后,而该项目的活动停止日期出现在该特定月份的最后一天之前(活动在单个月份内)。
(ITEM_START>= CalendarMonthStart and ITEM_STOP>= CalendarMonthEnd) - 项目活动开始日期在该特定月份的最后一天之前,在该特定月份的最后一天之后停止。
(ITEM_START<= CalendarMonthEnd and ITEM_STOP>= CalendarMonthEnd)
我的 proc sql 查询示例:
proc sql;
create table earned_activity as
select
a.ITEM_START,
a.ITEM_STOP,
b.MonthName,
b.CalendarMonthStart,
b.CalendarMonthEnd
/* Need to do something here */
from item_activity as a
left join calendar_table as b on
(ITEM_START<= CalendarMonthStart and ITEM_STOP>= CalendarMonthStart) or
(ITEM_START>= CalendarMonthStart and ITEM_STOP>= CalendarMonthEnd) or
(ITEM_START<= CalendarMonthEnd and ITEM_STOP>= CalendarMonthEnd)
;
quit;
将这三个案例加入我的数据后,我有一些示例数据,如下所示:
| ITEM_START | ITEM_STOP | MonthName | CalendarMonthStart | CalendarMonthEnd | num_days_active |
|---|---|---|---|---|---|
| 2021-01-06 | 2021-03-06 | Jan | 2021-01-01 | 2021-01-31 | 25 |
| 2021-01-06 | 2021-03-06 | Feb | 2021-02-01 | 2021-02-28 | 28 |
| 2021-01-06 | 2021-03-06 | Mar | 2021-03-01 | 2021-03-31 | 6 |
如您所见,对于笛卡尔积,我有一个项目在三个不同的日历月内都有活动。我想找到一种方法来获取该项目在每个月中存在的天数。我的第一个想法是在每个月的每一天执行笛卡尔积,并以某种方式计算那一天是否活跃,但我相信这可能会很快变得庞大而繁琐。是否有执行此类操作的好方法?
谢谢。
【问题讨论】:
标签: sql sql-server date cartesian-product proc-sql