通过SQL语句将行列数据转换为行

时间:2013-03-19 07:22:58

标签: sql oracle11g

我目前在这里使用oracle 11g是我面临的问题。我有一个SQL语句显示如下:

 SELECT A.LM_PERSON_ID
 ,A.LM_GRADE
 ,C.COURSE_STR
 ,Decode (A.LM_GRADE,'001','1','002','2','003','3') AS JOB_GRADE


 FROM PS_LM_PERSON_JOB A
,PS_LM_LPLN_LRN B 
,PS_LM_LPLN_DTL C 
,PS_LM_ENRLMT D
,PS_LM_CI_TBL E 


WHERE A.LM_EMPL_RCD='0'
AND A.LM_EFFSEQ=(SELECT Max(A1.LM_EFFSEQ) FROM PS_LM_PERSON_JOB A1 
WHERE A.LM_PERSON_ID=A1.LM_PERSON_ID AND A.EFFDT=A1.EFFDT 
AND A.LM_EMPL_RCD=A1.LM_EMPL_RCD)
AND A.LM_ACTIVE='Y'
AND A.EFFDT=(SELECT Max(A2.EFFDT) FROM PS_LM_PERSON_JOB A2 
WHERE A.LM_PERSON_ID=A2.LM_PERSON_ID AND A.LM_EMPL_RCD=A2.LM_EMPL_RCD 
AND A.LM_EFFSEQ=A2.LM_EFFSEQ) 
AND A.LM_PERSON_ID=B.LM_PERSON_ID (+)  
AND B.LM_LPLN_ID=C.LM_LPLN_ID (+) 
AND B.LM_PERSON_ID=C.LM_PERSON_ID (+)
AND C.LM_ENRLMT_ID=D.LM_ENRLMT_ID (+)  
AND D.LM_CI_ID=E.LM_CI_ID (+)
AND E.EFFDT=(SELECT Max(E1.EFFDT) FROM PS_LM_CI_TBL E1 WHERE E.LM_CI_ID=E1.LM_CI_ID)


ORDER BY A.LM_PERSON_ID

我想要的输出是:

  JOB GRADE   COURSE_STR.A    COURSE_STR.B    COURSE_STR.C 
    1              5               3               1
    2              4               2               2
    3              1               1               1

有人可以介意分享你的能力吗?非常感谢你。

1 个答案:

答案 0 :(得分:1)

查看PIVOT运算符,看看它是否解决了您的问题。

如果您提供了更多信息,我可以尝试编写您的查询。 也许一个没有转换的查询结果的简单例子就足够了。

编辑:

让我们想象一下测试表:

CREATE TABLE pivot_test (
  job_grade            NUMBER,
  course_str    VARCHAR2(1)
);

INSERT INTO pivot_test VALUES (1, 'A');
INSERT INTO pivot_test VALUES (1, 'B');
INSERT INTO pivot_test VALUES (1, 'C');
INSERT INTO pivot_test VALUES (1, 'A');
INSERT INTO pivot_test VALUES (1, 'C');
INSERT INTO pivot_test VALUES (2, 'A');
INSERT INTO pivot_test VALUES (2, 'B');
INSERT INTO pivot_test VALUES (2, 'C');
INSERT INTO pivot_test VALUES (2, 'A');
INSERT INTO pivot_test VALUES (3, 'A');
INSERT INTO pivot_test VALUES (3, 'A');
INSERT INTO pivot_test VALUES (3, 'A');
INSERT INTO pivot_test VALUES (3, 'A');
INSERT INTO pivot_test VALUES (3, 'A');
INSERT INTO pivot_test VALUES (3, 'B');
INSERT INTO pivot_test VALUES (3, 'B');
INSERT INTO pivot_test VALUES (3, 'A');
COMMIT;

我们的测试数据如下:

select * from pivot_test;

 JOB_GRADE COURSE_STR
---------- ----------
         1 A
         1 B
         1 C
         1 A
         1 C
         2 A
         2 B
         2 C
         2 A
         3 A
         3 A
         3 A
         3 A
         3 A
         3 B
         3 B
         3 A

17 rows selected

现在我们应用PIVOT操作,并获得结果:

SQL> SELECT *
  2        FROM pivot_test
  3        PIVOT(COUNT(course_str) FOR course_str IN('A' AS "COURSE_STR.A",
  4                                                  'B' AS "COURSE_STR.B",
  5                                                  'C' AS "COURSE_STR.C"));

 JOB_GRADE COURSE_STR.A COURSE_STR.B COURSE_STR.C
---------- ------------ ------------ ------------
         1            2            1            2
         2            2            1            1
         3            6            2            0

SQL>

我使用COUNT()作为聚合函数,但它可能是SUM(),MAX()等。

希望这有帮助