我试图理解这串代码以及它的意思,具体来说就是“ [^ |] +”

时间:2019-05-01 23:44:23

标签: sql oracle

我有一个同事通过电子邮件发送查询,该查询不再存在于该公司中,我正试图了解它在说什么。

, 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

我没有运气就试图研究这些陈述,请有人向我解释一下。

1 个答案:

答案 0 :(得分:4)

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

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

regexp_substr(c_qty, '[^|]+', 1, 1)返回的第一个字符串不包含c_qty列中的竖线,该字符串从该字符串的第一个字符开始。

此模式通常用于从管道分隔的列表中提取项目。例如,REGEXP_SUBSTR('A|B', '[^|]+', 1, 1)返回AREGEXP_SUBSTR('A|B', '[^|]+', 1, 2)返回B,依此类推。此类代码通常暗示数据模型存在问题;好像有人正在将非关系数据插入关系数据库。如果每列仅包含原子数据类型,则查询将更容易。额外的联接比解析列表容易得多。

更多信息