我有一张名为purchase_hs的表。在那张桌子里,我有一些等级日期。
+-----------+------------+------------+------------+------------+----------------+
| 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 |
+------------+------------+------------+------------+------------+----------------+
现在基于传输模式我需要在等级日期添加几天。如果传输模式='SEA'需要将28天添加到level1,level2和level3,如果transport_mode ='AIR'我需要添加14天到1级,2级和3级,剩下的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
我的出局将是
+------------+------------+------------+------------+------------+----------------+
| 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 |
+------------+------------+------------+------------+------------+----------------+
那我怎么能实现这个目标呢?请建议我
答案 0 :(得分: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
答案 1 :(得分: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