sql - 我试图理解这串代码和它在说什么,特别是这个'[^|]+'
问题描述
我有一位同事通过电子邮件向他发送了一个不再与公司合作的问题,我试图理解它在说什么。
, 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
我试过研究这些陈述,但没有运气,有人可以向我解释一下吗,谢谢。
解决方案
regexp_substr()
函数用于根据正则表达式提取子字符串。
[^|]+
是一个正则表达式,表示不包含竖线的字符串|
。
regexp_substr(c_qty, '[^|]+', 1, 1)
返回第一次出现的不包含垂直条的字符串,该c_qty
列从字符串的第一个字符开始。
此模式通常用于从管道分隔的列表中提取项目。例如,REGEXP_SUBSTR('A|B', '[^|]+', 1, 1)
returns A
、REGEXP_SUBSTR('A|B', '[^|]+', 1, 2)
returnsB
等。这种代码通常意味着数据模型存在问题;看起来有人正在将非关系数据插入关系数据库。如果每列仅包含原子数据类型,则查询会更容易。额外的连接比解析列表容易得多。
更多信息
推荐阅读
- android - Channel is unrecoverably broken and will be disposed
- sql - Query nodes above given record in tree
- java - 调用反射方法时参数数量未知
- python - 使用 TF-IDF 进行电影评分预测
- haskell - 修改 ReaderT 中的 ST 依赖环境 – `local` 函数的问题
- php - 使用 laravel 更新模式中的记录,而不是保存
- c++ - 在没有额外成员的情况下重新解释转换为派生类可以吗?
- python - R 中的 qgeom 和 Python 中的 scipy.stats.geom.ppf 有什么区别?
- solr - 如何在 SOLR 中包含 BengaliAnalyzer
- node.js - fs.readdirSync 找到 fs.statSync 无法读取的条目