1

次の表があります。

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;

私は何を間違えましたか?

4

2 に答える 2

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 |
于 2015-07-31T11:31:23.707 に答える