Oracle 10g—动态展平关系数据

Oracle 10g—动态展平关系数据,oracle,recordset,flatten,Oracle,Recordset,Flatten,我正在使用Oracle 10g。我有下面的关系结构,我想我需要把它展平,这样在客户端,网格视图会显示正确的列数 下面的表A可以配置为使用enabled字段启用或禁用其任何一条记录 TableB通过字段fk存储与TableA相关的计算值。对于docid 1,有为“nm1”和“nmn4”计算的值,但没有为“nm2”计算的值 我的问题是,对于TableA的特定配置,无论TableB中的docid是否具有计算值,我都需要返回一个记录集,该记录集包含TableA中已启用的全部记录。我希望动态创建的输出如下

我正在使用Oracle 10g。我有下面的关系结构,我想我需要把它展平,这样在客户端,网格视图会显示正确的列数

下面的表A可以配置为使用enabled字段启用或禁用其任何一条记录

TableB通过字段fk存储与TableA相关的计算值。对于docid 1,有为“nm1”和“nmn4”计算的值,但没有为“nm2”计算的值

我的问题是,对于TableA的特定配置,无论TableB中的docid是否具有计算值,我都需要返回一个记录集,该记录集包含TableA中已启用的全部记录。我希望动态创建的输出如下所示

有什么想法吗

TableA
id     nm     enabled
1     'nm1'   1
2     'nm2'   1
3     'nm3'   0
4     'nm4'   1


TableB
id     fk(A.id)     docid     value
1      1            1         .8
2      4            1         .6
3      1            2         .3
4      2            2         .4
5      4            2         .7
6      2            3         .6
7      4            3         .8

Output as records
1     'nm1'     .8       'nm2'     null     'nm4'     .6
2     'nm1'     .3       'nm2'     .4       'nm4'     .7
3     'nm1'     null     'nm2'     .6       'nm4'     .8

我相信有更好的方法,但这就是我想到的。另外,你发布的问题似乎有点模糊,所以我不能完全确定我回答的问题是否正确

首先,您需要获得一个包含所有可能结果的稀疏表,然后再次连接以获得值

这将输出为一列数据。如果不使用动态SQL或其他方法创建查询,就不可能为每个查询自定义列数

sys_connect_by_path用于将多行数据连接到一行中

with table_a as (
  select 1 as id, 'nm1' as nm, 1 as enabled from dual union all
  select 2 as id, 'nm2' as nm, 1 as enabled from dual union all
  select 3 as id, 'nm3' as nm, 0 as enabled from dual union all
  select 4 as id, 'nm4' as nm, 1 as enabled from dual
),
table_b as (
  select 1 as id, 1 as a_id, 1 as docid, 0.8 as value from dual union all
  select 2 as id, 4 as a_id, 1 as docid, 0.6 as value from dual union all
  select 3 as id, 1 as a_id, 2 as docid, 0.3 as value from dual union all
  select 4 as id, 2 as a_id, 2 as docid, 0.4 as value from dual union all
  select 5 as id, 4 as a_id, 2 as docid, 0.7 as value from dual union all
  select 6 as id, 2 as a_id, 3 as docid, 0.6 as value from dual union all
  select 7 as id, 4 as a_id, 3 as docid, 0.8 as value from dual 
),
cartesian_prod as (
  select b.docid, a.id, a.nm
  from 
    table_a a
    cross join (select distinct docid from table_b) b
  where a.enabled = 1
)
select 
  docid, 
  ltrim(max(sys_connect_by_path(nm || ' ' || value, ', ')), ', ') as value
from (
  select 
    c.docid, 
    c.nm, 
    nvl(to_char(b.value), 'null') as value, 
    row_number() over (partition by c.docid order by c.id) as rown
  from 
    cartesian_prod c 
    left outer join table_b b on (b.docid = c.docid and c.id = b.a_id)
)
start with rown = 1
connect by docid = prior docid and rown = prior rown + 1
group by docid

我相信有更好的方法,但这就是我想到的。另外,你发布的问题似乎有点模糊,所以我不能完全确定我回答的问题是否正确

首先,您需要获得一个包含所有可能结果的稀疏表,然后再次连接以获得值

这将输出为一列数据。如果不使用动态SQL或其他方法创建查询,就不可能为每个查询自定义列数

sys_connect_by_path用于将多行数据连接到一行中

with table_a as (
  select 1 as id, 'nm1' as nm, 1 as enabled from dual union all
  select 2 as id, 'nm2' as nm, 1 as enabled from dual union all
  select 3 as id, 'nm3' as nm, 0 as enabled from dual union all
  select 4 as id, 'nm4' as nm, 1 as enabled from dual
),
table_b as (
  select 1 as id, 1 as a_id, 1 as docid, 0.8 as value from dual union all
  select 2 as id, 4 as a_id, 1 as docid, 0.6 as value from dual union all
  select 3 as id, 1 as a_id, 2 as docid, 0.3 as value from dual union all
  select 4 as id, 2 as a_id, 2 as docid, 0.4 as value from dual union all
  select 5 as id, 4 as a_id, 2 as docid, 0.7 as value from dual union all
  select 6 as id, 2 as a_id, 3 as docid, 0.6 as value from dual union all
  select 7 as id, 4 as a_id, 3 as docid, 0.8 as value from dual 
),
cartesian_prod as (
  select b.docid, a.id, a.nm
  from 
    table_a a
    cross join (select distinct docid from table_b) b
  where a.enabled = 1
)
select 
  docid, 
  ltrim(max(sys_connect_by_path(nm || ' ' || value, ', ')), ', ') as value
from (
  select 
    c.docid, 
    c.nm, 
    nvl(to_char(b.value), 'null') as value, 
    row_number() over (partition by c.docid order by c.id) as rown
  from 
    cartesian_prod c 
    left outer join table_b b on (b.docid = c.docid and c.id = b.a_id)
)
start with rown = 1
connect by docid = prior docid and rown = prior rown + 1
group by docid

对我来说,这看起来像是一个亚种。您可以通过将表B与表A连接起来,然后限制启用(类似于
从B中选择B.*,其中B.A_id=A.id和A.enabled=1
)。然后你可以旋转它。

在我看来,这像是一个亚种。您可以通过将表B与表A连接起来,然后限制启用(类似于
从B中选择B.*,其中B.A_id=A.id和A.enabled=1
)。然后,您可以将其转为轴。

您可以提供有关输出的更多详细信息吗?我无法理解它是如何形成的…我只是重新格式化了输出。我想会更清楚一些。你能提供更多关于输出的细节吗?我无法理解它是如何形成的…我只是重新格式化了输出。我想这会更清楚。我使用了你上面给出的链接中的例子,它正是我所需要的。感谢所有回应者。我使用了你在上面给出的链接中的例子,它正是我所需要的。感谢所有回应的人。