Oracle 10g - 动态展平关系数据
我使用的是Oracle 10g。我有以下关系结构,我认为我需要将其展平,以便在客户端网格视图显示正确的列数。
下面的表 A 可以配置为使用启用字段启用或禁用其任何一条记录。
TableB通过字段fk存储与TableA相关的计算值。对于 docid 1,计算了“nm1”和“nmn4”的值,但没有计算“nm2”的值。
我的问题是,对于 TableA 的特定配置,我需要返回一个记录集,其中包含 TableA 中启用的记录的完整补充,无论 TableB 中的 docid 是否具有计算值。我希望动态创建的输出如下所示。
有什么想法吗?
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
I am using Oracle 10g. and I have the following relational structure that I think I need to flatten out so on the client side a grid view shows the correct number of columns.
TableA below can be configured to have any one of it's records enabled or disabled using the enabled field.
TableB stores calculated values related to TableA via the field fk. For docid 1 there are values calulated for 'nm1' and 'nmn4' but not for 'nm2'.
My problem is that for a particular configuration of TableA I need to return a record set having the full complement of enabled records in TableA whether or not a docid in TableB has a calculated value for it. The ouput I am looking to dynamically create is show below.
Any ideas?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来像 将查询转给我。您可以通过将表 B 与表 A 连接,然后限制启用(例如
select B.* from B, A where B.A_id = A.id and A.enabled = 1
)来进行过滤。然后你可以旋转它。This looks like a subspecies of pivot query to me. You can do the filtering by joining table B against table A, then restricting on enabled (something like
select B.* from B, A where B.A_id = A.id and A.enabled = 1
). You can then pivot that.我确信有更好的方法可以做到这一点,但这就是我想出的方法。另外,您发布的问题似乎有点含糊,所以我不完全确定我是否正确回答了问题。
首先,您需要获得所有可能结果的稀疏表,然后再次连接以获取值。
其输出为单列数据。如果不使用动态 SQL 或其他东西创建查询,实际上不可能为每个查询提供自定义的列数。
sys_connect_by_path 用于将多行数据连接成一行。
I'm sure there is a better way to do this but this is what I've come up with. Also, the question you have posted seems a little vague so I'm not entirely sure that I'm answering the question correctly.
Firstly you need to get a sparse table of all possible outcomes, then join again to get the values.
This has the output as a single column of data. It isn't really possible to have a custom number of columns per query without creating a query using dynamic SQL or something.
sys_connect_by_path is used to join the concatenate the multiple rows of data into a single row.