【问题标题】:Oracle Union All How are the results outputOracle Union All 结果输出如何
【发布时间】:2016-03-01 19:06:09
【问题描述】:

我们有一种情况,我们使用 3rd 方工具 (jitterbit) 来输出文件,并且我们需要文件具有标题行。所以我在想我们可以在 Oracle 视图中使用 union all 来为文件生成数据。

但我的问题是,Oracle 总是会先读取联合的顶部查询吗?

这将是我正在寻找的一个非常基本的示例

  select 'user.CustomAttribute.Client'
         ,'user.Email'
         ,'user.customAttribute.alternateEmail'
  from dual 
  UNION ALL 
  Select c.client 
         ,c.email
         ,c.alt_email
  from contact      

那么是否总是首先返回包含所有硬编码值的顶部?

【问题讨论】:

    标签: oracle union union-all


    【解决方案1】:

    将其下推到子查询中并添加一列来管理您的显式排序

    SELECT client, email, alt_email
    FROM (
     select 'user.CustomAttribute.Client' client 
             ,'user.Email' email
             ,'user.customAttribute.alternateEmail' alt_email
              -- here we add an aid to ordering, 1 comes first
             , 1 displayorder
      from dual 
      UNION ALL 
      Select c.client 
             ,c.email
             ,c.alt_email
              -- here we add an aid to ordering, 2 comes after our header
             , 2 displayorder
      from contact    )
    order by displayorder, client;
    

    【讨论】:

    • 好主意,感谢您的反馈。干杯!
    【解决方案2】:

    在 Oracle 中,此类查询没有保证或没有默认顺序。但是,如果您使用子查询,则可以管理所需的顺序,例如:

     select * from (select 'user.CustomAttribute.Client' as client 
         ,'user.Email' as email
         ,'user.customAttribute.alternateEmail' as alt_email
    from dual 
    UNION ALL 
    Select c.client 
         ,c.email
         ,c.alt_email
    from contact)
    order by client, email, alt_email;      
    

    阅读此链接了解更多详情:https://community.oracle.com/thread/2341048?tstart=0

    【讨论】:

    • 这里不需要子查询;您可以在两个联合分支之后进行排序,并引用第一个分支的列别名。但这不会将第一个联合分支结果放在结果集中,这似乎是 OP 想要的。您需要一个虚拟标志来使子查询实现任何目标,就像 Michael Broughton 的回答一样。
    猜你喜欢
    • 2018-07-08
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多