首页 > 解决方案 > 索引 - PL-SQL 中的匹配函数

问题描述

最近,我对从数据仓库获得的数据有一个非常具体的问题。问题正在解决,但我必须编辑我们的控制环境一段时间。

我们有收到发票的数据,但是由于某种原因,每张发票的信息被分成两行:第一行有重要的列unique_code_Avendor_number,第二行有重要的列unique_code_Bamount。所以每张发票都有非常具体的唯一代码,使用这个代码,我必须以某种方式连接两行的信息,如图所示。

我的桌子看起来如何与我希望看到的行如何

标签: sqloracle

解决方案


好吧,您可以使用聚合:

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

推荐阅读