Oracle案例查询

时间:2018-06-14 08:31:12

标签: oracle

我有一个聚合多个表的查询。表1,表2,表3和表4。 查询以

运行
SELECT TABLE1.COLUMN1, 
   RESULT.COLUMN1,
   RESULT.COLUMN2, 
   RESULT.COLUMN3,
   RESULT.SOMEAGGCOLUMN, 
   FROM (
            SELECT DISTINCT COLUMN1,COLUMN2,COLUMN3,
                CASE WHEN(SELECT COLUMN1 FROM TABLE2 
                            WHERE TABLE2.ID = TABLE3.TABLE2ID 
                            AND TABLE2.COLUMN2  = 'CRITERIA')
                        THEN (SELECT COLUMN2 FROM TABLE2)
                     ELSE 'DEFAULT VALUE'
                END AS SOMEAGGCOLUMN
            FROM TABLE2, TABLE3
             WHERE TABLE2.ID = TABLE3.TABLE2ID
             GROUPBY
             COLUMN1,
             COLUMN2,
             COLUMN3,
             SOMEAGGCOLUMN
        ) RESULT, TABLE1 WHERE

    RESULT.COLUMN1='CRITERIA'
    AND RESULT.COLUMN2 = 'CRITERIA'
    AND TABLE1.COLUMN1 = 'CRITERIA'
    GROUP BY
    TABLE1.COLUMN1, 
    RESULT.COLUMN1,
    RESULT.COLUMN2, 
    RESULT.COLUMN3,
    RESULT.SOMEAGGCOLUMN

我想以组合形式汇总和重新设置数据。在case select语句中添加AND rownum =1会返回正确的值,但我想返回不同的多个值,以便作为多行返回以与外部值组合。例如。如果内部查询返回2 TABLE2.COLUMN1 VALUES我想要记录说明。

       TABLE1.COLUMN1, 
       RESULT.COLUMN1,
       RESULT.COLUMN2, 
       RESULT.COLUMN3,
       RESULT.SOMEAGGCOLUMN = value one 

       and 

       TABLE1.COLUMN1, 
       RESULT.COLUMN1,
       RESULT.COLUMN2, 
       RESULT.COLUMN3,
       RESULT.SOMEAGGCOLUMN = value two. 

我可以使用查询实现吗?

  TABLE1
_____________________________
  ID COLUMN1 COLUMN2 COLUMN3
-----------------------------
  1   ABC     T6AD    OTHERM
  2   CDE     T7AD    ANOTHER
----------------------------

  TABLE2 
______________________________
  ID  COLUMN1 COLUMN2  COLUMN3
------------------------------
  1    ASA     T6AD    OTHERM
  2    AFS     T6AD    OTHERM
  3    AED     T7AD    ANOTHER
------------------------------

  TABLE3 
 ________________________________
  ID  TABLE2ID  COLUMN1 COLUMN2
 --------------------------------
  1      1       DETAIL  DETAIL2
  2      2       DETAIL3 DETAIL4
 -------------------------------

我想要实现的是使用条件得到表一结果然后从表2中获取记录,使用表1的非键列获取三个记录。样本结果。

对于每个T6AD FROM TABLE1,我会得到

 ABC T6AD OTHERM ASA DETAIL  DETAIL2
 ABC T6AD OTHERM AFS DETAIL3 DETAIL4

我知道这有点令人困惑,但想象这三个表的值将它们连接在一起,每个都是一个子集,进一步详细说明了存储其他细节的结构化数据。我想遍历到最后一个表来检索Major表中一条记录的详细信息。并且没有单键交叉,而是向下移动。

1 个答案:

答案 0 :(得分:2)

您似乎只是想要:

select t1.column1, t1.column2, t1.column3, t2.column1, t3.column1, t3.column2
from table1 t1
left join table2 t2 on t2.column2 = t1.column2 and t2.column3 = t1.column3
left join table3 t3 on t3.table2id = t2.id
where t1.column2 = 'T6AD';

将样本数据作为CTE:

with table1(id, column1, column2, column3) as (
  select 1, 'ABC', 'T6AD', 'OTHERM' from dual
  union all select 2, 'CDE', 'T7AD', 'ANOTHER' from dual
),
table2 (id, column1, column2, column3) as (
  select 1, 'ASA', 'T6AD', 'OTHERM' from dual
  union all select 2, 'AFS', 'T6AD', 'OTHERM' from dual
  union all select 3, 'AED', 'T7AD', 'ANOTHER' from dual
),
table3 (id, table2id, column1, column2) as (
  select 1, 1, 'DETAIL', 'DETAIL2' from dual
  union all select 2, 2, 'DETAIL3', 'DETAIL4' from dual
)
select t1.column1, t1.column2, t1.column3, t2.column1, t3.column1, t3.column2
from table1 t1
left join table2 t2 on t2.column2 = t1.column2 and t2.column3 = t1.column3
left join table3 t3 on t3.table2id = t2.id
where t1.column2 = 'T6AD';

COL COLU COLUMN3 COL COLUMN1 COLUMN2
--- ---- ------- --- ------- -------
ABC T6AD OTHERM  ASA DETAIL  DETAIL2
ABC T6AD OTHERM  AFS DETAIL3 DETAIL4

如果标准是T7AD而不是你得到:

COL COLU COLUMN3 COL COLUMN1 COLUMN2
--- ---- ------- --- ------- -------
CDE T7AD ANOTHER AED                

仍然不确定你指的是什么聚合......