【问题标题】:How to merge more than two select queries to single result set in oracle如何将两个以上的选择查询合并到oracle中的单个结果集
【发布时间】:2020-12-15 23:19:22
【问题描述】:

我需要将四个查询的结果集合并为单个结果输出。

例子:

Select sum(marks) total from table1 Where id > 1000 and id < 2000
Select sum(marks) total1 from table1 where id =>2000
Select sum(tables) totaltbl from table2 where id > 1000 and id < 2000
Select sum(tables) totaltbl1 from table2 where id => 2000

我需要以下格式的输出

Total total1 totaltbl totaltbl1
100.  2000.   10.      30

【问题讨论】:

    标签: sql oracle join aggregation


    【解决方案1】:

    一种选择是在表连接期间应用条件聚合:

    SELECT SUM(CASE WHEN t1.id >  1000 AND t1.id < 2000 THEN t1.marks END ) "Total",
           SUM(CASE WHEN t1.id >= 2000 THEN t1.marks END ) "Total1",
           SUM(CASE WHEN t2.id >  1000 AND t2.id < 2000 THEN t2.tables END ) "Totaltbl",
           SUM(CASE WHEN t2.id >= 2000 THEN t2.tables END ) "Totaltbl1"
      FROM table1 t1
     CROSS JOIN table2 t2     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      相关资源
      最近更新 更多