获取订单车辆的订单状态

Getting the order status of the vehicles on order

我正在尝试根据以下一组规则确定订购车辆的状态 - 是库存还是已售出,是否属于 BAM 活动以及售出或库存日期

  1. 如果数据栏orderstatus有*SLD,SLD或SOLD则评论栏为已售出;如果 orderstatus 列有 STOCK 或 *STOCK 或 STK 后跟日期(如 STK 05/13)或 *STK 后跟日期然后评论列是 stock

  2. 如果 orderstatus 列有 * 或 BAM 或任何日期(例如:5/13,05/17/2022),则 BAMYN 列将为 BAM

  3. 对于 Soldorstockdate 输出列,如果列 orderstatus 有一个日期,那么该日期将是 soldorstock 日期,否则 orddate 列中的日期将是 Soldorstockdate

请找到输入和输出的 DDL tables...你能帮我吗...

Input table

create table ##input
(segment varchar(20),
mmodel varchar(40),
brand varchar(30),
orderstatus varchar(100),
orddate date)

    insert into ##input values
    ('maka','M12E4','Nimg','*SLD 05/11/2022','5/1/2022'),
    ('sika','KL6781','Cheung','37141 SLD BAM','5/5/2022'),
    ('kloi','NB1290','Vloti','1277489 STK#39298.32831','5/4/2022'),
    ('Ping','BN1289','gower','36888 SLD FLOREN ANGEL','5/6/2022'),
    ('Melow','VB1901','operw','1286664 051222','5/10/2022'),
    ('Bekow','XC901','mewar','*SLD 5/14/22 Heman','5/3/2022'),
    ('Nakin','JH121','korew','STOCK','5/16/2022'),
    ('Verura','CV123','thilla','1287002 LONGMINT','5/12/2022'),
    ('Chaluli','BN8901','dora','STOCK BAM 5/17/22','5/11/2022'),
    ('Kroger','XC123','iops','*STOCK BAM 5/23/22','5/8/2022'),
    ('beqow','VB123','pirar','3902120 STOCK','5/20/2022'),
    ('Viast','NM41W','kolpe','SOLD BRANDON BOX 36790','5/15/2022'),
    ('Chimmin','BN123','tyrow','STK 5/13','5/3/2022'),
    ('Bellow','Vio23','Callow','*STK 5/13/22','5/5/2022'),
    ('Nalla','Krowmin','Gilqa','37938 STOCK 5/18/22 PER SARA','5/18/2022')

Output table

create table ##output
(segment varchar (20),
mmodel varchar(40),
brand varchar(30),
orderstatus varchar(100),
orddate date,
comment varchar(40),
BAMYN varchar(10),
soldorstockdate date)

insert into ##output values
('maka','M12E4','Nimg','*SLD 05/11/2022','5/1/2022','Sold','BAM','5/11/2022'),
('sika','KL6781','Cheung','37141 SLD BAM','5/5/2022','Sold','BAM','5/5/2022'),
('kloi','NB1290','Vloti','1277489 STK#39298.32831','5/4/2022','','',''),
('Ping','BN1289','gower','36888 SLD FLOREN ANGEL','5/6/2022','Sold','','5/6/2022'),
('Melow','VB1901','operw','1286664 051222','5/10/2022','','',''),
('Bekow','XC901','mewar','*SLD 5/14/22 Heman','5/3/2022','Sold','BAM','5/14/2022'),
('Nakin','JH121','korew','STOCK','5/16/2022','Stock','','5/16/2022'),
('Verura','CV123','thilla','1287002 LONGMINT','5/12/2022','','',''),
('Chaluli','BN8901','dora','STOCK BAM 5/17/22','5/11/2022','Stock','BAM','5/17/2022'),
('Kroger','XC123','iops','*STOCK BAM 5/23/22','5/8/2022','Stock','BAM','5/23/2022'),
('beqow','VB123','pirar','3902120 STOCK','5/20/2022','Stock','','5/20/2022'),
('Viast','NM41W','kolpe','SOLD BRANDON BOX 36790','5/15/2022','Sold','','5/15/2022'),
('Chimmin','BN123','tyrow','STK 5/13/2022','5/3/2022','Stock','BAM','5/13/2022'),
('Bellow','Vio23','Callow','*STK 5/13/22','5/5/2022','Stock','BAM','5/13/2022'),
('Nalla','Krowmin','Gilqa','37938 STOCK 5/18/22 PER SARA','5/18/2022','Stock','BAM','5/18/2022')

输入table

Segment mmodel brand orderstatus orddate
maka M12E4 Nimg *SLD 05/11/2022 5/1/2022
sika KL6781 Cheung 37141 SLD BAM 5/5/2022
kloi NB1290 Vloti 1277489 STK#39298.32831 5/4/2022
Ping BN1289 gower 36888 SLD FLOREN ANGEL 5/6/2022
Melow VB1901 operw 1286664 051222 5/10/2022
Bekow XC901 mewar *SLD 5/14/22 Heman 5/3/2022
Nakin JH121 korew STOCK 5/16/2022
Verura CV123 thilla 1287002 LONGMINT 5/12/2022
Chaluli BN8901 dora STOCK BAM 5/17/22 5/11/2022
Kroger XC123 iops *STOCK BAM 5/23/22 5/8/2022
beqow VB123 pirar 3902120 STOCK 5/20/2022
Viast NM41W kolpe SOLD BRANDON BOX 36790 5/15/2022
Chimmin BN123 tyrow STK 5/13 5/3/2022
Bellow Vio23 Callow *STK 5/13/22 5/5/2022
Nalla Krowmin Gilqa 37938 STOCK 5/18/22 PER SARA 5/18/2022

输出table

Segment mmodel brand orderstatus orddate comment BAM-Yes or no Soldorstockdate
maka M12E4 Nimg *SLD 05/11/2022 5/1/2022 Sold BAM 5/11/2022
sika KL6781 Cheung 37141 SLD BAM 5/5/2022 Sold BAM 5/5/2022
kloi NB1290 Vloti 1277489 STK#39298.32831 5/4/2022
Ping BN1289 gower 36888 SLD FLOREN ANGEL 5/6/2022 Sold 5/6/2022
Melow VB1901 operw 1286664 051222 5/10/2022
Bekow XC901 mewar *SLD 5/14/22 Heman 5/3/2022 Sold BAM 5/14/2022
Nakin JH121 korew STOCK 5/16/2022 Stock 5/16/2022
Verura CV123 thilla 1287002 LONGMINT 5/12/2022
Chaluli BN8901 dora STOCK BAM 5/17/22 5/11/2022 Stock BAM 5/17/2022
Kroger XC123 iops *STOCK BAM 5/23/22 5/8/2022 Stock BAM 5/23/2022
beqow VB123 pirar 3902120 STOCK 5/20/2022 Stock 5/20/2022
Viast NM41W kolpe SOLD BRANDON BOX 36790 5/15/2022 Sold 5/15/2022
Chimmin BN123 tyrow STK 5/13/2022 5/3/2022 Stock BAM 5/13/2022
Bellow Vio23 Callow *STK 5/13/22 5/5/2022 Stock BAM 5/13/2022
Nalla Krowmin Gilqa 37938 STOCK 5/18/22 PER SARA 5/18/2022 Stock BAM 5/18/2022

我正在尝试找出如何提取 orderstatus 列中的日期并仅选择具有 STK 后跟日期的值作为评论中的库存

还在 orderstatus 列中查找格式仅为 mm/dd/yy 或 mm/dd/yyyy 或 mm/dd

的日期

更新

在 DB2 中尝试查询

    SELECT a.*, CASE WHEN REGEXP_COUNT(PUR_ORD,'SLD')>0 THEN 'Sold' 
      WHEN REGEXP_COUNT ('%STOCK%',PUR_ORD)>0 OR REGEXP_COUNT ('STK[0-9]/[0-9]',PUR_ORD)>0 THEN 'Stock' ELSE'' END AS comment 
             ,CASE WHEN REGEXP_COUNT('[0-9]/[0-9]',PUR_ORD)>0 OR LOCATE('*',PUR_ORD)>0 OR LOCATE('BAM',PUR_ORD)>0
                   THEN 'BAM' ELSE'' END AS BAMYN 
             ,CASE WHEN REGEXP_COUNT('[0-9]/[0-9]',PUR_ORD)>0 
                   THEN CAST(SUBSTRING(PUR_ORD,LOCATE('/',PUR_ORD)-2,5) AS DATE)
                   ELSE ORD_DATE  END AS soldorstockdate 


FROM table a

我收到以下错误

SQL 错误 [2201S]:[SQ20558] 函数 REGEXP_COUNT 的正则表达式字符串无效。

谢谢, 阿伦

试试这个。
根据你的描述,SOLDORSTOCKDATE 列不能包含空值,但它在你想要的结果中有空值,这很奇怪。

SELECT 
  A.*
, CASE 
    WHEN REGEXP_COUNT (ORDERSTATUS, 'S.*LD') <> 0 
      THEN 'Sold' 
    WHEN REGEXP_COUNT (ORDERSTATUS, 'STOCK|STK [0-9]') <> 0 
      THEN 'Stock' 
    ELSE '' 
  END AS comment 
, CASE 
    WHEN REGEXP_COUNT (ORDERSTATUS, '[0-9]/[0-9]|\*|BAM') <> 0
      THEN 'BAM' 
    ELSE '' 
  END AS BAMYN 
, COALESCE (DATE (TO_DATE (REGEXP_SUBSTR (ORDERSTATUS, '[0-9]{1,2}/[0-9]{1,2}') || '/2022', 'MM/DD/YYYY')), orddate)  
      AS soldorstockdate 
FROM ##input A
SEGMENT MMODEL BRAND ORDERSTATUS ORDDATE COMMENT BAMYN SOLDORSTOCKDATE
maka M12E4 Nimg *SLD 05/11/2022 2022-05-01 Sold BAM 2022-05-11
sika KL6781 Cheung 37141 SLD BAM 2022-05-05 Sold BAM 2022-05-05
kloi NB1290 Vloti 1277489 STK#39298.32831 2022-05-04 2022-05-04
Ping BN1289 gower 36888 SLD FLOREN ANGEL 2022-05-06 Sold 2022-05-06
Melow VB1901 operw 1286664 051222 2022-05-10 2022-05-10
Bekow XC901 mewar *SLD 5/14/22 Heman 2022-05-03 Sold BAM 2022-05-14
Nakin JH121 korew STOCK 2022-05-16 Stock 2022-05-16
Verura CV123 thilla 1287002 LONGMINT 2022-05-12 2022-05-12
Chaluli BN8901 dora STOCK BAM 5/17/22 2022-05-11 Stock BAM 2022-05-17
Kroger XC123 iops *STOCK BAM 5/23/22 2022-05-08 Stock BAM 2022-05-23
beqow VB123 pirar 3902120 STOCK 2022-05-20 Stock 2022-05-20
Viast NM41W kolpe SOLD BRANDON BOX 36790 2022-05-15 Sold 2022-05-15
Chimmin BN123 tyrow STK 5/13 2022-05-03 Stock BAM 2022-05-13
Bellow Vio23 Callow *STK 5/13/22 2022-05-05 Stock BAM 2022-05-13
Nalla Krowmin Gilqa 37938 STOCK 5/18/22 PER SARA 2022-05-18 Stock BAM 2022-05-18