首页 > 解决方案 > 我试图理解这串代码和它在说什么,特别是这个'[^|]+'

问题描述

我有一位同事通过电子邮件向他发送了一个不再与公司合作的问题,我试图理解它在说什么。

, case when regexp_substr(c_qty, '[^|]+', 1, 1) <> nvl(sum(cd.actl_qty),0)
    then regexp_substr(c_qty, '[^|]+', 1, 1) - nvl(sum(cd.actl_qty),0) else null end Curr_Var
, case when regexp_substr(c_qty, '[^|]+', 1, 1) is null then 'First Count'
    when regexp_substr(c_qty, '[^|]+', 1, 1) = nvl(sum(cd.actl_qty),0)
        then 'Processed'
    when regexp_substr(c_qty, '[^|]+', 1, 1) > 0 and (regexp_substr(c_qty, '[^|]+', 1, 2) > 0 or regexp_substr(c_qty, '[^|]+', 1, 2) is null) and sum(cd.actl_qty) is null
        then 'Recount'
    when regexp_substr(c_qty, '[^|]+', 1, 1) <> nvl(sum(cd.actl_qty),0)
        and regexp_substr(c_qty, '[^|]+', 1, 1) = case when LENGTH(c_qty)-LENGTH(REPLACE(c_qty,'|',''))>1 then regexp_substr(c_qty, '[^|]+', 1, 2) else null end
        then 'Confirmed'
    when regexp_substr(c_qty, '[^|]+', 1, 1) <> nvl(sum(cd.actl_qty),0)
        and (regexp_substr(c_qty, '[^|]+', 1, 2) = nvl(sum(cd.actl_qty),0)
                or regexp_substr(c_qty, '[^|]+', 1, 3) = nvl(sum(cd.actl_qty),0)
                or regexp_substr(c_qty, '[^|]+', 1, 4) = nvl(sum(cd.actl_qty),0)
                or regexp_substr(c_qty, '[^|]+', 1, 5) = nvl(sum(cd.actl_qty),0)
                or regexp_substr(c_qty, '[^|]+', 1, 6) = nvl(sum(cd.actl_qty),0))
        then 'Expire Recent'
    when regexp_substr(c_qty, '[^|]+', 1, 1) <> nvl(sum(cd.actl_qty),0)
        and case when LENGTH(c_qty)-LENGTH(REPLACE(c_qty,'|',''))>1 then regexp_substr(c_qty, '[^|]+', 1, 2) else null end is null
        then 'Recount'
    when regexp_substr(c_qty, '[^|]+', 1, 1) <> nvl(sum(cd.actl_qty),0)
        and regexp_substr(c_qty, '[^|]+', 1, 1) <> case when LENGTH(c_qty)-LENGTH(REPLACE(c_qty,'|',''))>1
        then regexp_substr(c_qty, '[^|]+', 1, 2) else null end then 'Recount' else null end Directive
, regexp_substr(c_qty, '[^|]+', 1, 1) as Last_Cnt
, case when LENGTH(c_qty)-LENGTH(REPLACE(c_qty,'|',''))>1 then regexp_substr(c_qty, '[^|]+', 1, 2) else null end as Prev_Cnt1
, case when LENGTH(c_qty)-LENGTH(REPLACE(c_qty,'|',''))>2 then regexp_substr(c_qty, '[^|]+', 1, 3) else null end as Prev_Cnt2
, case when LENGTH(c_qty)-LENGTH(REPLACE(c_qty,'|',''))>3 then regexp_substr(c_qty, '[^|]+', 1, 4) else null end as Prev_Cnt3
, case when LENGTH(c_qty)-LENGTH(REPLACE(c_qty,'|',''))>4 then regexp_substr(c_qty, '[^|]+', 1, 5) else null end as Prev_Cnt4
, case when LENGTH(c_qty)-LENGTH(REPLACE(c_qty,'|',''))>5 then regexp_substr(c_qty, '[^|]+', 1, 6) else null end as Prev_Cnt5

我试过研究这些陈述,但没有运气,有人可以向我解释一下吗,谢谢。

标签: sqloracle

解决方案


regexp_substr()函数用于根据正则表达式提取子字符串。

[^|]+是一个正则表达式,表示不包含竖线的字符串|

regexp_substr(c_qty, '[^|]+', 1, 1)返回第一次出现的不包含垂直条的字符串,该c_qty列从字符串的第一个字符开始。

此模式通常用于从管道分隔的列表中提取项目。例如,REGEXP_SUBSTR('A|B', '[^|]+', 1, 1)returns AREGEXP_SUBSTR('A|B', '[^|]+', 1, 2)returnsB等。这种代码通常意味着数据模型存在问题;看起来有人正在将非关系数据插入关系数据库。如果每列仅包含原子数据类型,则查询会更容易。额外的连接比解析列表容易得多。

更多信息


推荐阅读