使用Oracle将三个表与PIVOT结合使用

时间:2018-05-16 04:19:36

标签: sql oracle

我有三个Oracle SQL select查询返回以下结果。 首先选择查询返回结果:

 user_id | user_name |
---------|-----------|
     1   |   user_1  |
     2   |   user_2  |
     3   |   user_3  |
     4   |   user_4  |

第二个选择查询返回结果:

 exam_id | exam_name |
---------|-----------|
    1    |   exam_1  |
    2    |   exam_2  |
    3    |   exam_3  |

并且第三个选择查询返回结果:

 exam_id | user_id | exam_date |
---------|---------|-----------|
    1    |    1    |    2017   |
    1    |    2    |    2018   |
    1    |    3    |    2017   |
    2    |    3    |    2018   |

我想结合这些查询来获得结果:

 user_id | user_name | exam_1 | exam_2 | exam_3 |
---------|-----------|--------|--------|--------|
     1   |   user_1  |  2017  |        |        |
     2   |   user_2  |  2018  |        |        |
     3   |   user_3  |  2017  |  2018  |        |
     4   |   user_4  |        |        |        |

我会感激你的帮助吗?

谢谢@shrek帮助我。我设法为枢轴值创建变量,但无法将变量放在枢轴中。所以我得到了here的帮助,最终版本(11g)看起来像这样:

variable x REFCURSOR
DECLARE
    exam_ids   VARCHAR2(255);
BEGIN
    SELECT
        LISTAGG(''''
                  || exam_id
                  || ''' AS "'
                  || exam_name
                  || '"',',') WITHIN GROUP(
            ORDER BY
                exam_id ASC
        )
    INTO exam_ids
    FROM
        exam;

    OPEN :x FOR 'SELECT
        *
               FROM
        (
            SELECT
                u.user_id,
                u.user_name,
                e.exam_id,
                eu.exam_date
            FROM
                users u
                LEFT JOIN exam_user eu ON u.user_id = eu.user_id
                LEFT JOIN exam e ON e.exam_id = eu.exam_id
            ORDER BY
                u.user_id
        )
            PIVOT ( MAX ( exam_date )
                FOR exam_id
                IN ( ' || EXAM_IDS || ' )
            )
    ORDER BY
        1';
END;
/

print x

这适用于SQL Developer和SQL * Plus。但是在尝试使用PHP文件的数据库时却没有。为此我需要创建过程,然后可以从PHP文件中调用。尝试从PHP文件和分辨率使用上面的代码时出现Here

1 个答案:

答案 0 :(得分:2)

这应该让你去 -

CREATE TABLE users
    (user_id varchar2(9), user_name varchar2(11))
;

INSERT ALL 
    INTO users (user_id, user_name)
         VALUES ('1', 'user_1')
    INTO users (user_id, user_name)
         VALUES ('2', 'user_2')
    INTO users (user_id, user_name)
         VALUES ('3', 'user_3')
    INTO users (user_id, user_name)
         VALUES ('4', 'user_4')
SELECT * FROM dual
;

CREATE TABLE exam
    (exam_id varchar2(9), exam_name varchar2(11))
;

INSERT ALL 
    INTO exam (exam_id, exam_name)
         VALUES ('1', 'exam_1')
    INTO exam (exam_id, exam_name)
         VALUES ('2', 'exam_2')
    INTO exam (exam_id, exam_name)
         VALUES ('3', 'exam_3')
SELECT * FROM dual
;

CREATE TABLE exam_user
    (exam_id varchar2(9), user_id varchar2(9), exam_date varchar2(11))
;

INSERT ALL 
    INTO exam_user (exam_id, user_id, exam_date)
         VALUES ('1', '1', '2017')
    INTO exam_user (exam_id, user_id, exam_date)
         VALUES ('1', '2', '2018')
    INTO exam_user (exam_id, user_id, exam_date)
         VALUES ('1', '3', '2017')
    INTO exam_user (exam_id, user_id, exam_date)
         VALUES ('2', '3', '2018')
SELECT * FROM dual
;

查询 -

SELECT * FROM (
SELECT U.USER_ID, U.USER_NAME, E.EXAM_NAME,EU.EXAM_DATE
FROM USERS U, EXAM E, EXAM_USER EU
WHERE U.USER_ID = EU.USER_ID(+)
AND E.EXAM_ID(+) = EU.EXAM_ID
ORDER BY U.USER_ID
  )
  PIVOT (MAX(EXAM_DATE) FOR EXAM_NAME IN ('exam_1' as exam_1, 'exam_2' as exam_2,'exam_3' as exam_3))
  order by 1
;
  

输出 -

USER_ID USER_NAME   EXAM_1  EXAM_2  EXAM_3
1   user_1  2017    (null)  (null)
2   user_2  2018    (null)  (null)
3   user_3  2017    2018    (null)
4   user_4  (null)  (null)  (null)