I am using the query below to pull up a list of accounts and the optional codes that go with them. There are 95 codes for each account, not just the 2 I am showing in the results below.
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;
Here are the results
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
I need to create the results as a pivot table that looks like the table below.
(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)
I have not tried this before and I am stumped
I have accomplished this in the past using table alias so that you can join a table to itself. I have done it with 4 or 5 columns not 95 so there may be a better way that I am not aware of.