简体   繁体   English

提取数据sql的模式

[英]Extracting patterns of data sql

I am trying to determine if an item is sold or on stock based on checking of the pattern SLD and SOLD from orderstatus column otherwise it would be stock我正在尝试根据从 orderstatus 列中检查 SLD 和 SOLD 模式来确定商品是否已售出或有库存,否则它将是库存

Also if * or BAM is present in orderstatus column then BAMyesorno column would be BAM此外,如果 orderstatus 列中存在 * 或 BAM,则 BAMyesorno 列将是 BAM

For the field soldorstockdate the date from orderstatus column would be input if its present (of the format mm/dd/yyyy) otherwise the date from orddate would be input对于字段 soldorstockdate,如果存在(格式为 mm/dd/yyyy),则输入 orderstatus 列中的日期,否则输入 orddate 中的日期

orderstatus订单状态 orddate日期 comment评论 BAM-Yes or no BAM-是或否 Soldorstockdate库存日期
*SLD 05/11/2022 *SLD 05/11/2022 5/1/2022 2022 年 5 月 1 日 Sold BAM巴姆 5/11/2022 2022 年 5 月 11 日
*SOLD 05/15/2022 *已于 2022 年 5 月 15 日出售 5/8/2022 2022 年 5 月 8 日 Sold BAM巴姆 5/15/2022 2022 年 5 月 15 日
37141 SLD BAM 37141 SLD BAM 5/5/2022 2022 年 5 月 5 日 Sold BAM巴姆 5/5/2022 2022 年 5 月 5 日
*STOCK 05/16/2022 *股票 05/16/2022 5/3/2022 2022 年 5 月 3 日 Stock库存 BAM巴姆 5/16/2022 2022 年 5 月 16 日
1277489 STK#39298.32831 1277489 STK#39298.32831 5/4/2022 2022 年 5 月 4 日 Stock库存 5/4/2022 2022 年 5 月 4 日
36888 SLD FLOREN ANGEL 36888 SLD弗洛伦天使 5/6/2022 2022 年 5 月 6 日 Sold 5/6/2022 2022 年 5 月 6 日
11274848 11274848 5/5/2022 2022 年 5 月 5 日 Stock库存 5/5/2022 2022 年 5 月 5 日

I tried the below:我尝试了以下方法:

SELECT 
    *,
    CASE 
        WHEN INSTR('%SLD%', `ORDERSTATUS`) > 0 
            THEN 'Sold'
        WHEN INSTR('%SOLD%', `ORDERSTATUS`) > 0 
             
        ELSE'Stock' 
    END AS comment,
    CASE 
        WHEN INSTR('%[0-9]/[0-9]%', `ORDERSTATUS`) > 0 
             OR LOCATE('*', `ORDERSTATUS`) > 0 
             OR LOCATE('BAM', `ORDERSTATUS`) > 0
            THEN 'BAM' 
            ELSE'' 
    END AS BAMYN,
    CASE 
        WHEN INSTR('%[0-9]/[0-9]%', `ORDERSTATUS`) > 0 
            THEN CAST(SUBSTRING(`ORDERSTATUS`, LOCATE('/', `ORDERSTATUS`) - 2, 5)  AS DATE)
            ELSE `ORD_DATE` 
    END AS soldorstockdate 
FROM 
    table

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')

I could not seem to cover the short date without a year (the Chimmin entry with 5/13)我似乎无法涵盖没有一年的短日期( Chimmin条目与 5/13)

Try this modified example SQL, I believe that it should be pretty close to what is expected.试试这个修改后的示例 SQL,我相信它应该非常接近预期。 Though I also had confusion of empty string dates (with '' ) in the output example.尽管在输出示例中我也对空字符串日期(带有'' )感到困惑。

SELECT 
    *,
    CASE 
        WHEN REGEXP_LIKE(`ORDERSTATUS`, 'SLD|SOLD') > 0 
            THEN 'Sold'
        ELSE 'Stock' 
    END AS comment,
    CASE 
        WHEN REGEXP_INSTR(`ORDERSTATUS`, '[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}') > 0 
             OR LOCATE('*', `ORDERSTATUS`) > 0 
             OR LOCATE('BAM', `ORDERSTATUS`) > 0
            THEN 'BAM' 
            ELSE '' 
    END AS BAMYN,
    CASE 
        WHEN REGEXP_LIKE(`ORDERSTATUS`, '[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}') > 0 
            THEN DATE_FORMAT(
                     STR_TO_DATE(
                         REGEXP_SUBSTR(`ORDERSTATUS`, '[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}'), 
                         IF(LENGTH(REGEXP_SUBSTR(`ORDERSTATUS`, '[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}')) < 10, '%m/%d/%y', '%m/%d/%Y')
                     ),
                     '%Y-%m-%d'
                 )
            ELSE `ORDDATE` 
    END AS soldorstockdate
FROM 
    input

That can be also be tried with this example dbfiddle .也可以使用此示例 dbfiddle进行尝试。

Feel free to ask any questions about the handling.随时询问有关处理的任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM