sql - 索引 - PL-SQL 中的匹配函数
问题描述
最近,我对从数据仓库获得的数据有一个非常具体的问题。问题正在解决,但我必须编辑我们的控制环境一段时间。
我们有收到发票的数据,但是由于某种原因,每张发票的信息被分成两行:第一行有重要的列unique_code_A,vendor_number,第二行有重要的列unique_code_B,amount。所以每张发票都有非常具体的唯一代码,使用这个代码,我必须以某种方式连接两行的信息,如图所示。
解决方案
好吧,您可以使用聚合:
select date_key, invoice_type,
max(case when unique_code_b is null then unique_code_a end) as unique_code_a,
max(unique_code_b) as unique_code_b,
max(case when unique_code_b is null then vendor_number end) as vendor_number,
max(case when unique_code_b is not null then amount end) as amount
from t
group by date_key, invoice_type;
编辑:
如果可以使用唯一代码进行匹配,那么我建议:
select date_key, invoice_type,
coalesce(unique_code_a, unique_code_b) as unique_code,
max(case when unique_code_b is null then vendor_number end) as vendor_number,
max(case when unique_code_b is not null then amount end) as amount
from t
group by date_key, invoice_type, coalesce(unique_code_a, unique_code_b);
推荐阅读
- docker - ML docker 容器本地设置完成。现在如何使用 Kubernetes 在共享云平台上进行部署?
- http - 状态 302 - 错误文档移动(HTTP 高级)
- c++ - 关于匿名命名空间和内部链接的标准是什么?
- laravel - Laravel - 如何将外键值显示为视图刀片的标题
- oracle - 带游标的plsql过程
- javascript - 删除重定向 onclick
- python - 如何从 ipynb 文件运行另一个 ipynb 文件?
- c# - Asp.net Core 获取 Bearer error="invalid_token", error_description="签名无效"
- python - 如何使用 groupby 或 resample 对每小时数据进行下采样,以根据 python 中的年日小时索引对数据进行分组?
- rabbitmq - 组件崩溃时 RabbitMQ 崩溃