我正试图转换我的数据的当前格式 1 转换成图像格式 2. 正如你所看到的数据目前被分割成两行,每一个cust_id都有一个代码,但我希望它在一行。一个给定的代码的open和response是相互排斥的1,即代码A的custx没有匹配的分配值1 & 1的open和response,但可以有0 & 0,1 & 0或0 & 1.我使用的是Oracle SQL Developer 19.2.1。先谢谢你
当前SQL数据格式
希望的SQL数据格式
尝试以下,这里是 演示.
select
cust_key,
min(case when code = 'A' then open end) as A_open,
min(case when code = 'B' then open end) as B_open,
min(case when code = 'A' then replied end) as A_replied,
min(case when code = 'B' then replied end) as B_replied
from yourTable
group by
cust_key
输出。
|cust_key A_open B_open A_replied B_replied |
---------------------------------------------------
| cust1 0 0 1 0 |
| cust2 0 0 1 1 |
---------------------------------------------------
你可以使用 "PIVOT",如下所示。
with xxx as (
select 'cust1' custkey,'A' code, 0 opened,1 replied from dual
union all
select 'cust1' custkey,'B' code, 0 opened,0 replied from dual
union all
select 'cust2' custkey,'A' code, 0 opened,1 replied from dual
union all
select 'cust2' custkey,'B' code, 0 opened,1 replied from dual
)
SELECT * FROM
(
select xxx.* from XXX
)
PIVOT
(
sum(opened) as OPENED,sum(replied) REPLIED FOR (CODE) IN ('A' AS A,'B' AS B)
)
group by custkey,a_opened,a_REPLIED,b_opened,b_REPLIED