如何在Oracle中使用子查询?

时间:2015-07-31 10:40:19

标签: oracle stored-procedures syntax plsql sys-refcursor

我有下表:

table1
-------------------------
 date      |  ID  | name
-------------------------
 13-jul-15 |   1  |  abc
 13-jul-15 |   2  |  abc
 14-jul-15 |   1  |  def
 13-jul-15 |   3  |  abc
 15-jul-15 |   3  |  def
 ...

我想要做的是匹配ID并表示如下:

 date1     | name | date2     | name | ID 
------------------------------------------ 
 13-jul-15 | abc  | 14-jul-15 | def  | 1   
 13-jul-15 | abc  |           |      | 2  
 13-jul-15 | abc  | 15-jul-15 | def  | 3
 ...

我使用了以下代码,但没有得到结果。

CREATE PROCEDURE get_details ( oresults1 OUT SYS_REFCURSOR ) AS
BEGIN
  SELECT *
    FROM ((SELECT date, ID FROM table1 WHERE name= "abc") T1
           UNION ALL
          (SELECT date, ID FROM table1 WHERE name= "def") T2 
         )
   WHERE T1.ID= T2.ID
   ORDER BY ID;
END;

我做错了什么?

2 个答案:

答案 0 :(得分:3)

以下是两种选择:

with table1 as (select to_date('13/07/2015', 'dd/mm/yyyy') dt, 1 id, 'abc' name from dual union all
                select to_date('13/07/2015', 'dd/mm/yyyy') dt, 2 id, 'abc' name from dual union all
                select to_date('14/07/2015', 'dd/mm/yyyy') dt, 1 id, 'def' name from dual union all
                select to_date('13/07/2015', 'dd/mm/yyyy') dt, 3 id, 'abc' name from dual union all
                select to_date('15/07/2015', 'dd/mm/yyyy') dt, 3 id, 'def' name from dual)
-- end of mimicking your table1. See below for the query
select t1.dt date1,
       t1.name name1,
       t2.dt date2,
       t2.name name2,
       t1.id
from   table1 t1
       left outer join table1 t2 on (t1.id = t2.id and t1.name = 'abc' and t2.name = 'def')
where  t1.name = 'abc'
order by t1.id;

DATE1      NAME1 DATE2      NAME2         ID
---------- ----- ---------- ----- ----------
13/07/2015 abc   14/07/2015 def            1
13/07/2015 abc                             2
13/07/2015 abc   15/07/2015 def            3

with table1 as (select to_date('13/07/2015', 'dd/mm/yyyy') dt, 1 id, 'abc' name from dual union all
                select to_date('13/07/2015', 'dd/mm/yyyy') dt, 2 id, 'abc' name from dual union all
                select to_date('14/07/2015', 'dd/mm/yyyy') dt, 1 id, 'def' name from dual union all
                select to_date('13/07/2015', 'dd/mm/yyyy') dt, 3 id, 'abc' name from dual union all
                select to_date('15/07/2015', 'dd/mm/yyyy') dt, 3 id, 'def' name from dual)
-- end of mimicking your table1. See below for the query
select t1.dt date1,
       t1.name name1,
       t2.dt date2,
       t2.name name2,
       t1.id
from   (select id, dt, name from table1 where name = 'abc') t1
       left outer join (select id, dt, name from table1 where name = 'def') t2 on (t1.id = t2.id)
where  t1.name = 'abc'
order by t1.id;

DATE1      NAME1 DATE2      NAME2         ID
---------- ----- ---------- ----- ----------
13/07/2015 abc   14/07/2015 def            1
13/07/2015 abc                             2
13/07/2015 abc   15/07/2015 def            3

答案 1 :(得分:2)

您还可以使用Oracle中提供的数据透视功能

WITH table_(date#, id#, name#) AS 
(SELECT to_date('13-jul-15', 'dd-mon-yy'), 1,  'abc' FROM dual UNION all 
 SELECT to_date('13-jul-15', 'dd-mon-yy'), 2,  'abc' FROM dual UNION all
 SELECT to_date('14-jul-15', 'dd-mon-yy'), 1,  'def' FROM dual UNION all
 SELECT to_date('13-jul-15', 'dd-mon-yy'), 3,  'abc' FROM dual UNION all
 SELECT to_date('15-jul-15', 'dd-mon-yy'), 3,  'def' FROM dual)
--------
-- End of data preparation
--------
SELECT * 
  FROM table_
 PIVOT (MIN(date#) AS date#, MIN(name#) AS NAME# FOR name# IN ('abc' AS ABC, 'def' AS DEF));

输出

| ID# |              ABC_DATE# | ABC_NAME# |              DEF_DATE# | DEF_NAME# |
|-----|------------------------|-----------|------------------------|-----------|
|   1 | July, 13 2015 00:00:00 |       abc | July, 14 2015 00:00:00 |       def |
|   2 | July, 13 2015 00:00:00 |       abc |                        |           |
|   3 | July, 13 2015 00:00:00 |       abc | July, 15 2015 00:00:00 |       def |