在Sql中为日期列概括更新查询

时间:2017-08-31 18:45:17

标签: sql sql-server sql-update

我有一个名为Pedidos的SQL Server表,其结构如下:

enter image description here

每个月(最后几天)我都要运行这些查询:

UPDATE PEDIDOS
SET PedFchAct = CONVERT(DATETIME, '2017-08-28 00:00:00', 102), 
    PrgFchAct = CONVERT(DATETIME, '2017-08-28 00:00:00', 102)
WHERE     
    (PedFecCap = CONVERT(DATETIME, '2017-09-01 00:00:00', 102))

UPDATE PEDIDOS
SET PedFchAct = CONVERT(DATETIME, '2017-08-29 00:00:00', 102), 
    PrgFchAct = CONVERT(DATETIME, '2017-08-29 00:00:00', 102)
WHERE (PedFecCap = CONVERT(DATETIME, '2017-09-02 00:00:00', 102))

UPDATE PEDIDOS
SET PedFchAct = CONVERT(DATETIME, '2017-08-30 00:00:00', 102), 
    PrgFchAct = CONVERT(DATETIME, '2017-08-30 00:00:00', 102)
WHERE 
    (PedFecCap > CONVERT(DATETIME, '2017-09-02 00:00:00', 102)) 
    AND (PedFecCap <= CONVERT(DATETIME, '2017-09-30 00:00:00', 102))

我的想法如下,我必须将寄存器按PedFecCap分成三部分。

过滤日期为:

  • PedFecCap = 1 / nextmonth / 2017
  • PedFecCap = 2 / nextmonth / 2017,
  • lastdayOfnextMonth / nextmonth / 2017&lt; = PedFecCap&gt; 2 / nextmonth / 2017

然后我必须更新PedFchAct和PrgFchAct,并为每个过滤的段设置当前月份的三个不同日期。例如:

对于 PedFecCap = 1 / nextmonth / 2017,PedFchAct和PrgFchAct = 28 / currentmonth / 2017

对于 PedFecCap = 2 / nextmonth / 2017,PedFchAct和PrgFchAct = 29 / currentmonth / 2017

对于lastdayOfnextMonth / nextmonth / 2017&lt; = PedFecCap &gt; 2 / nextmonth / 2017,PedFchAct和PrgFchAct = 30 / currentmonth / 2017

我需要概括这个查询并消除固定值,所以每个月我只运行脚本而不是重写查询。

2 个答案:

答案 0 :(得分:1)

示例数据

pedfeccap               pedfchaact  prgfchact
2017-07-01 00:00:00.000 NULL        NULL
2017-07-02 00:00:00.000 NULL        NULL
2017-07-03 00:00:00.000 NULL        NULL
2017-07-28 00:00:00.000 NULL        NULL
2017-07-29 00:00:00.000 NULL        NULL
2017-07-30 00:00:00.000 NULL        NULL
2017-07-31 00:00:00.000 NULL        NULL
2017-08-01 00:00:00.000 NULL        NULL
2017-08-02 00:00:00.000 NULL        NULL
2017-08-03 00:00:00.000 NULL        NULL
2017-08-27 00:00:00.000 NULL        NULL
2017-08-28 00:00:00.000 NULL        NULL
2017-08-29 00:00:00.000 NULL        NULL
2017-08-30 00:00:00.000 NULL        NULL
2017-08-31 00:00:00.000 NULL        NULL
2017-09-01 00:00:00.000 NULL        NULL
2017-09-02 00:00:00.000 NULL        NULL
2017-09-03 00:00:00.000 NULL        NULL
2017-09-27 00:00:00.000 NULL        NULL
2017-09-28 00:00:00.000 NULL        NULL
2017-09-29 00:00:00.000 NULL        NULL
2017-09-30 00:00:00.000 NULL        NULL

您可以使用每个日期的第一天和最后一天计算

UPDATE pedidos
   SET pedfchaact = CASE WHEN pedfeccap = DATEADD(MONTH, DATEDIFF(MONTH, 0, pedfeccap), 0)
                         THEN DATEADD(MONTH, DATEDIFF(MONTH, -1, pedfeccap) -2, 0) + 27
                         WHEN pedfeccap = DATEADD(MONTH, DATEDIFF(MONTH, 0, pedfeccap), 0) + 1
                         THEN DATEADD(MONTH, DATEDIFF(MONTH, -1, pedfeccap) -2, 0) + 28
                         ELSE DATEADD(MONTH, DATEDIFF(MONTH, -1, pedfeccap) -2, 0) + 29
                     END,
       prgfchact = CASE WHEN pedfeccap = DATEADD(MONTH, DATEDIFF(MONTH, 0, pedfeccap), 0)
                        THEN DATEADD(MONTH, DATEDIFF(MONTH, -1, pedfeccap) -2, 0) + 27
                        WHEN pedfeccap = DATEADD(MONTH, DATEDIFF(MONTH, 0, pedfeccap), 0) + 1
                        THEN DATEADD(MONTH, DATEDIFF(MONTH, -1, pedfeccap) -2, 0) + 28
                        ELSE DATEADD(MONTH, DATEDIFF(MONTH, -1, pedfeccap) -2, 0) + 29
                    END;

结果

pedfeccap               pedfchaact              prgfchact
2017-07-01 00:00:00.000 2017-06-28 00:00:00.000 2017-06-28 00:00:00.000
2017-07-02 00:00:00.000 2017-06-29 00:00:00.000 2017-06-29 00:00:00.000
2017-07-03 00:00:00.000 2017-06-30 00:00:00.000 2017-06-30 00:00:00.000
2017-07-28 00:00:00.000 2017-06-30 00:00:00.000 2017-06-30 00:00:00.000
2017-07-29 00:00:00.000 2017-06-30 00:00:00.000 2017-06-30 00:00:00.000
2017-07-30 00:00:00.000 2017-06-30 00:00:00.000 2017-06-30 00:00:00.000
2017-07-31 00:00:00.000 2017-06-30 00:00:00.000 2017-06-30 00:00:00.000
2017-08-01 00:00:00.000 2017-07-28 00:00:00.000 2017-07-28 00:00:00.000
2017-08-02 00:00:00.000 2017-07-29 00:00:00.000 2017-07-29 00:00:00.000
2017-08-03 00:00:00.000 2017-07-30 00:00:00.000 2017-07-30 00:00:00.000
2017-08-27 00:00:00.000 2017-07-30 00:00:00.000 2017-07-30 00:00:00.000
2017-08-28 00:00:00.000 2017-07-30 00:00:00.000 2017-07-30 00:00:00.000
2017-08-29 00:00:00.000 2017-07-30 00:00:00.000 2017-07-30 00:00:00.000
2017-08-30 00:00:00.000 2017-07-30 00:00:00.000 2017-07-30 00:00:00.000
2017-08-31 00:00:00.000 2017-07-30 00:00:00.000 2017-07-30 00:00:00.000
2017-09-01 00:00:00.000 2017-08-28 00:00:00.000 2017-08-28 00:00:00.000
2017-09-02 00:00:00.000 2017-08-29 00:00:00.000 2017-08-29 00:00:00.000
2017-09-03 00:00:00.000 2017-08-30 00:00:00.000 2017-08-30 00:00:00.000
2017-09-27 00:00:00.000 2017-08-30 00:00:00.000 2017-08-30 00:00:00.000
2017-09-28 00:00:00.000 2017-08-30 00:00:00.000 2017-08-30 00:00:00.000
2017-09-29 00:00:00.000 2017-08-30 00:00:00.000 2017-08-30 00:00:00.000
2017-09-30 00:00:00.000 2017-08-30 00:00:00.000 2017-08-30 00:00:00.000

答案 1 :(得分:0)

您应该可以使用CASE声明执行此操作:

UPDATE PEDIDOS
SET
    PedFchAct = CASE
                    WHEN PedFecCap = '2017-09-01' THEN '2017-08-28'
                    WHEN PedFecCap = '2017-09-02' THEN '2017-08-29'
                    ELSE '2017-08-30'
                END,
    PrgFchAct = CASE
                    WHEN PedFecCap = '2017-09-01' THEN '2017-08-28'
                    WHEN PedFecCap = '2017-09-02' THEN '2017-08-29'
                    ELSE '2017-08-30'
                END
WHERE
    PedFecCap BETWEEN '2017-09-01' AND '2017-09-30'