【发布时间】:2019-03-21 00:35:16
【问题描述】:
想知道是否有人可以指导我正确的方向。
我的数据表结构如下:
table2_type char (1)
table2_key char (40)
table2_reftype char (20)
table2_seq char (10)
table2_ref char (50)
和数据行类似(逗号分隔仅供阅读):
I,123456789,typea,0000000001,abc
I,123456789,typeb,0000000002,999
I,123456789,typec,0000000003,9z9
I,123456789,typed,0000000004,zyx
I,123456789,typee,0000000005,qwe
I,987654321,typea,0000000006,bcd
I,987654321,typeb,0000000007,444
我现在必须做的是获取相同 table_key 值的所有 table_ref 值(为便于阅读而间隔):
select table1_bkey,
nvl((case when (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad('typea',20)) is not null then (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad('typea',20)) else (select table2_ref from table2 where table2_key = table1_alt and table2_reftype = rpad('typea',20)) end),' ') as REF_A,
nvl((case when (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad('typeb',20)) is not null then (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad('typeb',20)) else (select table2_ref from table2 where table2_key = table1_alt and table2_reftype = rpad('typeb',20)) end),' ') as REF_B,
nvl((case when (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad('typec',20)) is not null then (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad('typec',20)) else (select table2_ref from table2 where table2_key = table1_alt and table2_reftype = rpad('typec',20)) end),' ') as REF_C,
nvl((case when (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad('typed',20)) is not null then (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad('typed',20)) else (select table2_ref from table2 where table2_key = table1_alt and table2_reftype = rpad('typed',20)) end),' ') as REF_D,
nvl((case when (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad('typee',20)) is not null then (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad('typee',20)) else (select table2_ref from table2 where table2_key = table1_alt and table2_reftype = rpad('typee',20)) end),' ') as REF_E
from table1 where table1_bkey = '123456789';
我目前每个 table2_key 最多可以有 25 个不同的 table2_reftype,所以我必须做 25 个 case 语句。如果/当添加新的 table2_reftype 时,我必须做新的案例陈述。
我的问题是,有没有一种方法可以获取每个 table2_key 的所有 table2_reftype 值,而无需执行多个 case 语句,例如:
仅供说明之用,请勿以任何方式将文字视为代码!!
while table2_key = table1_bkey
get table2_reftype
then for each table2_reftype
nvl((case when (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad(<table2_reftype variable>,20)) is not null then (select table2_ref from table2 where table2_key = table1_bkey and table2_reftype = rpad(<table2_reftype variable>,20)) else (select table2_ref from table2 where table2_key = table1_alt and table2_reftype = rpad(<table2_reftype variable>,20)) end),' ') as REF_<table2_reftype variable>
where table1_bkey = '123456789';
第一次发帖,希望我已经提供了足够的信息。
【问题讨论】:
-
顺便问一下,您确定要将所有
char值空白填充到最大值吗?似乎是一个奇怪的业务需求。 -
嗨,在我找到问题的解决方案之前,我必须做些什么。