【问题标题】:Selects returning different tables based on condition in other table in Oracle根据Oracle中其他表中的条件选择返回不同的表
【发布时间】:2017-09-01 09:26:26
【问题描述】:

想法是我有一个临时表:

Type     | Sources
Invoices | 44,22,11
Billings | 99,90,12,34

在我的应用程序中(交互式报告内有交互式报告的顶点),用户单击一行图标:账单发票。在我的 plsql 查询中,我得到值 #type##sources# 作为结果(它们将被顶点注入适当的值)。我想实现这样的目标:

select
    case when #type# = 'Billings'
        then (select from table_billings where table_billings.sources = #sources#)
    when #type# = 'Invoices'
        then (select from table_invoices where table_invoices.sources = #sources#)

我对如何做到这一点有很大的问题,因为事实上,我的主要选择来自 NOTHING,只有内部的有一些表。另一方面,我不能从 dual 使用,因为列数和行数可能会有所不同。有什么想法吗?

【问题讨论】:

  • 我的建议是规范化您的表格并删除该 CSV 数据。它将使查询和维护成为一场噩梦。
  • @TimBiegeleisen 不幸的是,这个 Sources 列是在 groupby 期间生成的,我对此无能为力。

标签: oracle plsql oracle-apex


【解决方案1】:

如果列可能不同,那么这在 SQL 中是不可能的。您必须在应用程序级别解决它。当用户点击 billings 时,然后从 table_billings 中选择,否则从 table_invoices 中选择。

但是,如果您可以统一常见的输出列,那么您可以使用联合,例如::

select col1, col2, col3 
  from table_billings
  where #type# = 'Billings' and sources = #sources#
union all
select colA, colB, colC 
  from table_invoices
  where #type# = 'Invoices' and sources = #sources#

两个表的输出列必须是相同的类型。

【讨论】:

    【解决方案2】:

    您可以为发票、账单等提供单独的交互式报告,并使用“页面项目 P1_TYPE 的值为 X”形式的服务器端条件有条件地为所选类型呈现适当的报告。

    【讨论】:

    • 我不能这样做,因为该查询已在现有的交互式报告中使用,在插件嵌套报告中。
    猜你喜欢
    • 2019-12-13
    • 1970-01-01
    • 2015-03-28
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多