【问题标题】:Add fixed number of days to date column in SQL Server table and determine weekends在 SQL Server 表中添加固定天数列并确定周末
【发布时间】:2015-12-12 23:13:13
【问题描述】:

我在 SQL Server 中有一个表,比如表 1,如下所示:

Location   No:of Days
 Timbuktu     7
 Valhalla     5
   Spain      20

此表列出了配送到这些地点的天数。

我有另一个表,Table2 如下所示:

Location     Part     Ready_To_Ship_Date     Estimated_Delivery_Date

Valhalla     XXXX           26-Dec-15    
  Spain      YYYY           01-Jan-16
Valhalla     ZZZZ           15-Jan-16
Timbuktu     AAAA           15-Dec-15

现在我需要将表 1 中给出的位置的天数添加到表 2 中该位置的相应 Ready_To_Ship 记录日期,获取 Estimated_Delivery_Date 并检查交货日期是星期六还是星期日。如果是,则将日期设置为下周一。对于一些地点来说,这将是周五和周六,因为你们都知道世界各地的周末是不一样的。

我在 SO:With SQL Server add x number of days to a date in sql but also pass in the number of business days in a week 中找到了这个帖子

但我无法弄清楚如何将它用于我的用例。请在这方面指导我。

提前感谢您提供的帮助。

【问题讨论】:

  • Bingle“SQL Server 日历表”。这还可以让您识别所有非工作日,包括节假日。

标签: sql sql-server date


【解决方案1】:

您可以采取两步走的方法。不是最有效的,但对您来说可能已经足够了。将第二次更新中的值更改为与您想要的周末天数相对应:

update Table2
set Estimated_Delivery_Date =
    dateadd(
        day,
        (select "No:of Days" from Table1 t1 where t1.Location = Table2.Location),
        Ready_To_Ship_Date
    );

update Table2
set Estimated_Delivery_Date =
    dateadd(
        day,
        case
            when datepart(weekday, Estimated_Delivery_Date) = 7 then 2
            when datepart(weekday, Estimated_Delivery_Date) = 1 then 1
            else 0 /* redundant with the where */
        end,
        Estimated_Delivery_Date
    )
where datepart(weekday, Estimated_Delivery_Date) in (1, 7);

这并不是说不能一步完成;它只是变得更难阅读:

update Table2
set Estimated_Delivery_Date =
    dateadd(
        day,
        coalesce((select "No:of Days" from Table1 t1 where t1.Location = Table2.Location), 0)
        + case
            datepart(weekday, dateadd(
                day,
                (select "No:of Days" from Table1 t1 where t1.Location = Table2.Location),
                Ready_To_Ship_Date
            ))
            when 7 then 2
            when 1 then 1
            else 0
        end,
        Ready_To_Ship_Date
    )
    end

第二个查询还通过coalesce() 处理 Table1 中的失败查找。我不认为你会有这个问题。

还要注意,有些人会在更新中使用inner join 语法而不是重复子查询。虽然这是非标准的,但它有很多怪癖,这使它成为避免的一件好事。优化器可能仍然可以解决所有问题。

现在,如果您需要动态计算各个国家/地区的周末工作日,您需要在某处查找该数据。如果您没有找到通用解决方案,它也可能取决于您服务器的区域设置。

您可能更喜欢更健壮的东西,但一个简单的选择是假设您将列 WeekendDay1WeekendDay2 添加到 Table1 使用美国日编号和匹配的服务器设置 @@datefirst

update Table2
set Estimated_Delivery_Date =
    dateadd(
        day,
        coalesce((select "No:of Days" from Table1 t1 where t1.Location = Table2.Location), 0)
        + case
            datepart(weekday, dateadd(
                day,
                (select "No:of Days" from Table1 t1 where t1.Location = Table2.Location),
                Ready_To_Ship_Date
            ))
            when (select WeekendDay1 from Table1 t1 where t1.Location = Table2.Location)
              then 2
            when (select WeekendDay2 from Table1 t1 where t1.Location = Table2.Location)
              then 1
            else 0
        end,
        Ready_To_Ship_Date
    )
    end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    相关资源
    最近更新 更多