2017-08-31 54 views
1

我有這種結構命名爲Pedidos SQL Server表:期廣義更新查詢在SQL的日期列

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 < = PedFecCap> 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 < = PedFecCap> 2/nextmonth/2017,PedFchAct和PrgFchAct = 30/currentmonth/2017

我需要概括這個查詢和消除固定值,所以每個月我只運行腳本,而不是重寫查詢。

回答

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' 
+0

這是本月的最佳選擇,但下個月它將會改變,而08和09(8月和9月)將分別爲09和10。這就是泛化的意義。 –

0

的樣本數據

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 
+0

請仔細閱讀我的問題。平臺和團隊(三段更新)只在當天有所不同。這個月本月是一樣的。 –

+0

我再讀一遍。我會稍後嘗試編輯查詢。 –

+0

請參閱更新的SQL UPDATE語句和結果 –