2015-03-31 42 views
0

我有一個名爲purchase_hs的表。在那張表中我有一些關卡日期。使用case語句添加日期間隔mysql

+-----------+------------+------------+------------+------------+----------------+ 
    | level1  | level2  | level3  | level4  | level5  | transport_mode | 
    +------------+------------+------------+------------+------------+----------------+ 
    | 2015-02-15 | 15.02.2015 | 21.02.2015 | 19.03.2015 | 16.03.2015 | SEA   | 
    | 2015-02-17 | 17.02.2015 | 17.02.2015 |   | 16.03.2015 | AIR   | 
    | 2015-02-17 | 17.02.2015 | 17.02.2015 |   | 16.03.2015 | AIR   | 
    | 2015-02-17 | 17.02.2015 | 17.02.2015 |   | 16.03.2015 | AIR   | 
    +------------+------------+------------+------------+------------+----------------+ 

現在根據運輸方式,我需要一些日子添加到水平dates.If傳輸模式=「下海」需要28天增加1級,2級以及3級,如果TRANSPORT_MODE =「AIR」我需要將14天添加到level1,level2和level3,並且剩餘的level4和level5在顯示時保持不變。 我試圖通過使用下面的查詢來獲取。但它不適合我。

SELECT level1,level2,level3,level4,level5,transport_mode, 
CASE WHEN transport_mode = 'SEA' 
THEN 
DATE_ADD(level1, INTERVAL 28 DAY) level1, DATE_ADD(STR_TO_DATE(level2, '%d.%m.%Y'), INTERVAL 28 DAY) level2, 
DATE_ADD(STR_TO_DATE(level3, '%d.%m.%Y'), INTERVAL 28 DAY) level3 
ELSE 
transport_mode = 'AIR' 
THEN 
DATE_ADD(level1, INTERVAL 14 DAY) level1, DATE_ADD(STR_TO_DATE(level2, '%d.%m.%Y'), INTERVAL 14 DAY) level2, 
DATE_ADD(STR_TO_DATE(level3, '%d.%m.%Y'), INTERVAL 14 DAY) level3 
END 
FROM purchase_hs 

我desierd出認沽將

+------------+------------+------------+------------+------------+----------------+ 
| level1  | level2  | level3  | level4  | level5  | transport_mode | 
+------------+------------+------------+------------+------------+----------------+ 
| 2015-03-15 | 15.03.2015 | 2015-03-18 | 19.03.2015 | 16.03.2015 | SEA   | 
| 2015-03-04 | 04.03.2015 | 04.03.2015 |   | 16.03.2015 | AIR   | 
| 2015-03-04 | 04.03.2015 | 04.03.2015 |   | 16.03.2015 | AIR   | 
| 2015-03-04 | 04.03.2015 | 04.03.2015 |   | 16.03.2015 | AIR   | 
+------------+------------+------------+------------+------------+----------------+ 

因此,如何能實現我這個放出來?請建議我

回答

1

您需要爲每列單獨提供CASE表達式,但不能在THEN子句中有多個列。另外,您不能在ELSE子句中添加條件,它會自動處理與WHEN子句中的任何一個都不匹配的所有條件。

SELECT level1,level2,level3,level4,level5,transport_mode, 
    CASE WHEN transport_mode = 'SEA' 
     THEN DATE_ADD(level1, INTERVAL 28 DAY) 
     ELSE DATE_ADD(level1, INTERVAL 14 DAY) 
    END level1, 
    CASE WHEN transport_mode = 'SEA' 
     THEN DATE_ADD(STR_TO_DATE(level2, '%d.%m.%Y'), INTERVAL 28 DAY) 
     ELSE DATE_ADD(STR_TO_DATE(level2, '%d.%m.%Y'), INTERVAL 14 DAY) 
    END level2, 
    CASE WHEN transport_mode = 'SEA' 
     THEN DATE_ADD(STR_TO_DATE(level3, '%d.%m.%Y'), INTERVAL 28 DAY) 
     ELSE DATE_ADD(STR_TO_DATE(level3, '%d.%m.%Y'), INTERVAL 24 DAY) 
    END level3 
FROM purchase_hs 

你可以避開所有的重複使用子查詢:

SELECT level1,level2,level3,level4,level5,transport_mode, 
    DATE_ADD(level1, INTERVAL inter DAY) level1, 
    DATE_ADD(STR_TO_DATE(level2, '%d.%m.%Y'), INTERVAL inter DAY) level2, 
    DATE_ADD(STR_TO_DATE(level3, '%d.%m.%Y'), INTERVAL inter DAY) level3 
FROM (SELECT level1,level2,level3,level4,level5,transport_mode, 
      CASE transport_mode 
       WHEN 'SEA' THEN 28 
       WHEN 'AIR' THEN 14 
       WHEN 'PIGEON' THEN 60 
       ELSE 90 
      END inter 
     FROM purchase_hs) x 
+0

非常感謝,這對我的作品,但如果我有2種以上的運輸方式的話,我怎麼能提case語句? – user3408779 2015-03-31 04:57:42

+0

你可以有多個'WHEN'子句。 – Barmar 2015-03-31 05:00:59

+1

'CASE transport_mode'SEA'then 28'AIR'then 14'CARRIER_PIGEON'60 60 ELSE 90 END' – Barmar 2015-03-31 05:01:49

0

您可以創建一個模板臺物理/暫時的,簡單地將兩個表如下

你的數據

DECLARE @data AS TABLE(
level1 DATE, 
level2 DATE, 
level3 DATE, 
level4 DATE, 
level5 DATE, 
mode VARCHAR(10)) 
INSERT INTO @data VALUES('2015-02-15','2015-02-15','2015-02-21','2015-03-19','2015-03-16','SEA') 
INSERT INTO @data VALUES('2015-02-17','2015-02-17','2015-02-17',null,'2015-03-16','AIR') 
INSERT INTO @data VALUES('2015-02-17','2015-02-17','2015-02-17',null,'2015-03-16','AIR')  
INSERT INTO @data VALUES('2015-02-17','2015-02-17','2015-02-17',null,'2015-03-16','AIR')  

根據您的邏輯的臨時模板

DECLARE @template AS TABLE(
level1 INT, 
level2 INT, 
level3 INT, 
level4 INT, 
level5 INT, 
mode VARCHAR(10)) 
INSERT INTO @template VALUES(28,28,28,0,0,'SEA') 
INSERT INTO @template VALUES(14,14,14,0,0,'AIR') 

只要加入以上兩個表

SELECT * FROM @data 
SELECT * FROM @template 

SELECT DATEADD(DAY,B.level1,A.level1) AS level1, 
DATEADD(DAY,B.level2,A.level2) AS level2, 
DATEADD(DAY,B.level3,A.level3) AS level3, 
DATEADD(DAY,B.level4,A.level4) AS level4, 
DATEADD(DAY,B.level5,A.level5) AS level5, 
A.mode FROM @data A INNER JOIN @template B ON A.mode = B.mode