Home > Net >  How to write case expression while pivoting the data
How to write case expression while pivoting the data

Time:12-25

CREATE TABLE details_1 (
    e_id    NUMBER(10),
    e_name  VARCHAR2(30),
    CONSTRAINT pk_details_1_e_id PRIMARY KEY ( e_id )
);

insert into details_1 values(11,'A');


CREATE TABLE ques_ans (
    ques_ans_id  NUMBER(10),
    ref_ques_id  NUMBER(10),
    ref_ans_id   NUMBER(10),
    ref_ans_value VARCHAR2(100),
    e_id         NUMBER(10),
    CONSTRAINT pk_ques_ans PRIMARY KEY ( ques_ans_id ),
    CONSTRAINT fk_ques_ans FOREIGN KEY ( e_id )
        REFERENCES details_1 ( e_id ),
        constraint fk_ques_and_ques_id foreign key(ref_ques_id)
        references ques_ref (ques_id)
);

insert into ques_ans values(1,3,1,11,null);
insert into ques_ans values(2,2,2,11,null);
insert into ques_ans values(3,4,1,11,null);
insert into ques_ans values(4,23,1,11,11);

CREATE TABLE ques_ref (
    ques_id     NUMBER(10),
    code        VARCHAR2(50),
    code_label  VARCHAR2(100),
    constraint pk_ques_ref primary key(ques_id)
);

insert into ques_ref values(3,'changes_exist','Any known changes');
insert into ques_ref values(2,'E_Clubbed','E_id clubbed with other');
insert into ques_ref values(4,'E_impacted','E impacted by other');
insert into ques_ref values(23,'E_Clubbed_with_other','E clubbed with other E');

CREATE TABLE ans_ref (
    ref_ans_id  NUMBER(10),
    code        VARCHAR2(10),
    code_value  VARCHAR2(30)
);

insert into ans_ref values(1,'R_Yes','Yes');
insert into ans_ref values(2,'R_No','No');

commit;

My Attempt :

select  d.e_id,
        max(case qa.ref_ques_id when 3 then ar.code_value end) changes_exist,
        max(case qa.ref_ques_id when 2 then ar.code_value end) E_Clubbed,
        max(case qa.ref_ques_id when 4 then ar.code_value end) E_impacted,
--need to write case expression here
  from      details_1 d
        join
            ques_ans qa
          on d.e_id = qa.e_id
        join ans_ref ar
          on ar.ref_ans_id = qa.ref_ans_id
  group by d.e_id

I got stuck in the below requirement:

I need to check if ref_ques_id of ques_ans table is 23 then it should display ref_ans_value from the same table i.e ques_ans

For example: In the table ques_ans for ques_ans_id 4 ref_ques_id is 23 then in this case it will display ref_ans_value i.e 11 in the column ref_ans_value

How can I write case expressions while pivoting the data? I am wondering if we can do it using case expression or is there any other way to achieve this?

Expected Output:

 ------ --------------- ----------- ------------ ------------------ 
| E_ID | CHANGES_EXIST | E_CLUBBED | E_IMPACTED | E_CLUBBED_WITH_E |
 ------ --------------- ----------- ------------ ------------------ 
|   11 | Yes           | No        | Yes        |               11 |
 ------ --------------- ----------- ------------ ------------------ 

CodePudding user response:

You can achieve that with further aggregations.

CASE
    WHEN MAX(ques_ans.ref_ques_id) = '23' THEN MAX(COALESCE(ques_ans.ref_ans_value, 0))
    ELSE -4
END

There are two problems you will need to sort out:

1

You artificially aggregated things in the SELECT clause to prevent technical problems, but you might need to change this, depending on how your actual data looks alike (not the one shown in the question, but in reality). If you have answers with different references, then you might want to change your database structure to better reflect reality.

2

You did not tell us what should happen when the value of ques_id is NOT 23, so I added an ad-hoc value in the ELSE. You will need to change it to the one you actually need.

enter image description here

  • Related