【问题标题】:How to write a query in Oracle which combines/joins the results of two queries into one如何在 Oracle 中编写一个查询,将两个查询的结果组合/连接成一个
【发布时间】:2020-11-11 21:11:47
【问题描述】:

如果我的INPUT DATA如下图:

id1Number id2String   id3String     blob1       blob2       last_updated_timestamp
----------------------------------------------------------------------------------
1         1-1         11-11                     ...         2020/01/01
2         2-1         22-11         ...         ...         2020/01/02
3         3-1                       ...                     2020/01/01
4         4-1         44-11         ...                     2020/01/05
5         5-1         55-11                                 2020/01/01
6         6-1         66-11                     ...         2020/02/02
7         7-1         77-11         ...                     2020/02/03

预期输出 我想编写一个 Query3,它将 Query1 和 Query2 的结果组合在一个查询中,并将得到如下所示的输出

select id1Number, id2String, id3String 
from table
where ...

id1Number id2String   id3String
-------------------------------
1         1-1                  
4                     44-11    
5         5-1         55-11    

查询1

select id1Number, id2String 
from table 
where blob1 is NULL and 
      last_updated_timestamp >= to_timestamp('2020-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and 
      last_updated_timestamp <  to_timestamp('2020-02-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS');

id1Number id2String
-------------------
1         1-1      
5         5-1      

查询2

select id1Number, id3String 
from table 
where blob2 is NULL and 
      id3String is NOT NULL and 
      last_updated_timestamp >= to_timestamp('2020-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and 
      last_updated_timestamp <  to_timestamp('2020-02-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS');

id1Number id3String
-------------------
4         44-11    
5         55-11    

【问题讨论】:

    标签: sql oracle plsql where-clause


    【解决方案1】:

    嗯。 . .过滤和案例表达:

    select id1Number,
           (case when blob1 is null then id2String end),
           (case when (blob2 is NULL then id3String  end)
    from table 
    where (blob1 is NULL or
           (blob2 is NULL and id3String is NOT NULL)
          ) and
          last_updated_timestamp >= to_timestamp('2020-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and 
          last_updated_timestamp <  to_timestamp('2020-02-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS');
    

    您可以在timestampsdates 之间轻松切换,因此where 可以简化为:

    where (blob1 is NULL or
           (blob2 is NULL and id3String is NOT NULL)
          ) and
          last_updated_timestamp >= date '2020-01-01' and 
          last_updated_timestamp <  date '2020-02-01';
    

    当然,您也可以使用 timestamp 文字。

    【讨论】:

    • 谢谢@Gordon Linoff。它对我有用。真的很感激
    【解决方案2】:

    我想你想要case 表达式:

    select id1number,
        case when blob1 is null then id2string end as id2string,
        case when blob2 is null then id3string end as id3string
    from mytable
    where 
        last_updated_timestamp >= date '2020-01-01'
        and last_updated_timestamp < date '2020-01-02'
        and (blob1 is null or (blob2 is null and id3String is not null))
    

    请注意,我将过滤逻辑更改为使用文字日期而不是 to_timestamp(),这会稍微缩短查询时间。

    【讨论】:

    • 谢谢@GMB。它对我有用。真的很感激
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 2019-04-05
    • 2015-10-08
    • 2021-09-06
    • 2016-10-05
    • 1970-01-01
    相关资源
    最近更新 更多