0

我正在使用下面的查询来提取帐户列表以及与它们一起使用的可选代码。每个帐户有 95 个代码,而不仅仅是我在下面的结果中显示的 2 个。

SELECT DISTINCT Ref1.ACCOUNT_ID as Acct_Numb,
Current_Date as DATA_DATE,
Cat.OPTIONAL_CTGRY_CD As Code,
Cat.OPTIONAL_CTGRY_CD || ' - ' || Cat.OPTIONAL_CTGRY_NM AS Code_Combo,
Class.OPTIONAL_CLASS_CD as Code_Answer,
Class.OPTIONAL_CLASS_NM as Code_Answer_Desc

FROM xxxxx.zzzzzz_OPT_REF Ref1

LEFT JOIN xxxxx.zzzzzz_OPT_CATEGORY Cat
ON xxxxx.zzzzzz_OPT_REF.OPTIONAL_CTGRY_CD = xxxxx.zzzzzz_OPT_CATEGORY.OPTIONAL_CTGRY_CD

LEFT JOIN xxxxx.zzzzzz_OPT_CLASS Class
ON xxxxx.zzzzzz_OPT_REF.OPTIONAL_CLASS_CD = xxxxx.zzzzzz_OPT_CLASS.OPTIONAL_CLASS_CD
AND xxxxx.zzzzzz_OPT_CATEGORY.OPTIONAL_CTGRY_CD = xxxxx.zzzzzz_OPT_CLASS.OPTIONAL_CTGRY_CD

LEFT JOIN xxxxx.HRTVACT_PCS Acct
ON xxxxx.ACCOUNT_ID = Acct.ACCOUNTID

WHERE xxxxx.ACCOUNTSTATUS = 'OPEN' AND xxxxx.ACCOUNTID = '123456' OR xxxxx.ACCOUNTID = '654321'

ORDER BY ACCT_NUMB ASC, CODE ASC;

这是结果

DATA_DATE   ACCT_NUMB   CODE   CODE_COMBO      CODE_ANSWER  CODE_ANSWER_DESC
11/8/2016     123456     1    1 - Reporting        0     NOT APPLICABLE
11/8/2016     123456     2    2 - System           4     SYSTEM 2
11/8/2016     654321     1    1 - Reporting        3     APPLIED
11/8/2016     654321     2    2 - System           3     N/A

我需要将结果创建为如下表所示的数据透视表。

                       (CODE)  (CODE_COMBO)    (CODE)    (CODE_COMBO)   
DATA_DATE   ACCT_NUMB     1    1 - Reporting     2        2 - System
11/8/2016    123456       0    NOT APPLICABLE    4         SYSTEM 2    (CODE_ANSWER)/(CODE_ANSWER_DESC)
11/8/2016    654321       3    APPLIED           3         N/A         (CODE_ANSWER)/(CODE_ANSWER_DESC)

我以前没有试过这个,我很难过

4

1 回答 1

0

我过去使用表别名完成了此操作,以便您可以将表连接到自身。我用 4 或 5 列而不是 95 列完成了它,所以可能有更好的方法我不知道。

于 2016-11-08T15:51:10.390 回答