我有一个名为purchase_hs
的表,我有不同类型的等级日期和不同类型的运输模式和数量,如下所示。现在我的要求是我需要考虑每行只有一个级别的日期列。假设我们有level1
且余下都是空的,那么我们需要根据传输模式添加28天(sea
= 28天,air
= 14天,truck
= 5天,transport mode
为空,然后为28天),并显示level1
日期数量。如果我们有level1
日期和level2
日期,剩下的都是空的,则需要level2
日期数量,如果我们有level1
,level2
和{ {1}}日期level3
,level4
为空,我们需要获取level3数量,依此类推。对于level5
和level4
日期,我们不需要根据运输添加任何天数。我们只需要考虑level5
,level1
和level2
日期的传输模式。为了达到这个目的,我的查询是,但它不起作用。
level3
表:
SELECT level1,
level2,
level3,
level4,
level5,
trasprt Quantity,
CASE
WHEN level1 != ''
AND level2 = ''
AND level3 = ''
AND level4 = ''
AND level5 = ''
AND trasprt = 'SEA'
THEN DATE_ADD(level1, INTERVAL 28 DAY) level1
WHEN level1 != ''
AND level2 != ''
AND level3 = ''
AND level4 = ''
AND level5 = ''
AND trasprt = 'AIR'
THEN DATE_ADD(STR_TO_DATE(level2, '%d.%m.%Y'), INTERVAL 14 DAY) level2
WHEN level1 != ''
AND level2 != ''
AND level3 != ''
AND level4 = ''
AND level5 = ''
AND trasprt = 'TRK'
THEN DATE_ADD(STR_TO_DATE(level3, '%d.%m.%Y'), INTERVAL 5 DAY) level3
WHEN level1 != ''
AND level2 != ''
AND level3 != ''
AND level4 != ''
AND level5 = ''
THEN level4
WHEN level1 != ''
AND level2 != ''
AND level3 != ''
AND level4 != ''
AND level5 != ''
THEN level5
ELSE
END
FROM purchase_hs where stock_id = '4046228100449'
我的结果表将是:如果我只有level1和level2日期且传输模式是SEA(需要将28天添加到结果级别日期),那么结果将是
+------------+------------+------------+------------+--------+----------+----------------+---------------+
| level1 | level2 | level3 | level4 | level5 | Stock Qty | transport | StockId|
+------------+------------+------------+------------+--------+----------+----------------+---------------+
| 2015-01-17 | 15.04.2015 | | | | 150.0000 | SEA | 4046228100449 |
| 2015-02-17 | 20.01.2015 | 17.02.2015 | | | 150.0000 | SEA | 4046228100450 |
| 2015-01-15 | 05.04.2015 | 05.04.2015 | 03.05.2015 | | 2.0000 | | 4046228100451 |
+------------+------------+------------+------------+--------+----------+----------------+---------------+
如果我只有level1日期,剩下的所有级别日期都是emtpy而且传输模式是AIR(需要将14天添加到生成的级别日期),那么结果将是
+------------+----------+---------------+
| level2 | StockQty | StockId |
+------------+----------+---------------+
| 13.05.2015 | 150.0000 | 4046228100449 |
+------------+----------+---------------+
如果我只有level1 date,level2 date,level3 date和level4,level5为空,那么传输模式为TRK(需要将5天添加到生成的级别日期),然后结果将是
+------------+----------+---------------+
| level1 | menge_pe | ean |
+------------+----------+---------------+
| 2015-01-31 | 150.0000 | 4046228100449 |
+------------+----------+---------------+
如果我只有level1日期,level2 date,level3 date和level4有值,level5是(我们不需要考虑28/14/5天基于level4和level5日期的传输模式)然后结果将是
+------------+----------+---------------+
| level3 | menge_pe | ean |
+------------+----------+---------------+
| 22.02.2015 | 150.0000 | 4046228100449 |
+------------+----------+---------------+
那么任何人都可以指导我做错的地方吗?