【问题标题】:Count of Work Days Between Two Date Columns两个日期列之间的工作日数
【发布时间】:2020-11-20 11:15:28
【问题描述】:

我正在尝试提出一个交货期的工作日数。注意:我无法定义函数。

我有一张表格,上面有送货详情:

+--------+---------------+---------------+
| Rec_Id | Date_Received | Date_Promised |
+--------+---------------+---------------+
| 1      | 2020-07-01    | 2020-07-07    |
+--------+---------------+---------------+
| 2      | 2020-07-15    | 2020-07-08    |
+--------+---------------+---------------+

我有一个如下的工作日表(T 表示它是工作日):

+---------------+----------+
| CALENDAR_DATE | WORK_DAY |
+---------------+----------+
| 2020-07-01    | T        |
+---------------+----------+
| 2020-07-02    | F        |
+---------------+----------+
| 2020-07-03    | F        |
+---------------+----------+
| 2020-07-04    | F        |
+---------------+----------+
| 2020-07-05    | F        |
+---------------+----------+
| 2020-07-06    | F        |
+---------------+----------+
| 2020-07-07    | T        |
+---------------+----------+
| 2020-07-08    | T        |
+---------------+----------+
| 2020-07-09    | T        |
+---------------+----------+
| 2020-07-10    | T        |
+---------------+----------+
| 2020-07-11    | F        |
+---------------+----------+
| 2020-07-12    | F        |
+---------------+----------+
| 2020-07-13    | T        |
+---------------+----------+
| 2020-07-14    | T        |
+---------------+----------+
| 2020-07-15    | T        |
+---------------+----------+

结果如下:

+--------+---------------+---------------+----------+
| Rec_Id | Date_Received | Date_Promised | Days_Off |
+--------+---------------+---------------+----------+
| 1      | 2020-07-01    | 2020-07-07    | -1       |
+--------+---------------+---------------+----------+
| 2      | 2020-07-15    | 2020-07-08    | 5        |
+--------+---------------+---------------+----------+

提前致谢

【问题讨论】:

    标签: sql sql-server join count subquery


    【解决方案1】:

    您可以使用横向连接、子查询和条件逻辑:

    select 
        d.*,
        case when d.date_received > d.date_promised
            then (
                select count(*) 
                from work_days w 
                where 
                    w.work_day = 'T' 
                    and w.calendar_date >= d.date_promised 
                    and w.calendar_date < d.date_received
            )
            else (
                select - count(*) 
                from work_days w 
                where 
                    w.work_day = 'T' 
                    and w.calendar_date >= d.date_received 
                    and w.calendar_date < d.date_promised
            )
        end as days_off
    from delivery_details d
    

    您可以在子查询中移动条件逻辑以稍微缩短代码 - 尽管我怀疑它可能效率较低:

    select 
        d.*,
        (
            select case when date_received > date_promised then 1 else -1 end * count(*) 
            from work_days w 
            where 
                w.work_day = 'T' 
                and (
                    (w.calendar_date >= d.date_promised and w.calendar_date < d.date_received)
                    or (w.calendar_date >= d.date_received and w.calendar_date < d.date_promised)
                )
        ) as days_off
    from delivery_details d
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 2021-06-02
      • 2010-09-20
      • 2023-03-09
      • 2015-07-21
      相关资源
      最近更新 更多