[英]How to write case expression while pivoting the data
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 ie ques_ans
我需要检查ref_ques_id
表的ques_ans
是否为 23,那么它应该显示来自同一个表的ref_ans_value
,即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
ie 11 in the column ref_ans_value
例如:在ques_ans_id
4 的表ques_ans
中, ref_ques_id
为 23,那么在这种情况下,它将在ref_ans_value
列中显示ref_ans_value
即 11
How can I write case expressions while pivoting the data?如何在旋转数据时编写 case 表达式? I am wondering if we can do it using case expression or is there any other way to achieve this?我想知道我们是否可以使用案例表达式来做到这一点,或者有没有其他方法可以实现这一点?
Expected Output:预期 Output:
+------+---------------+-----------+------------+------------------+
| E_ID | CHANGES_EXIST | E_CLUBBED | E_IMPACTED | E_CLUBBED_WITH_E |
+------+---------------+-----------+------------+------------------+
| 11 | Yes | No | Yes | 11 |
+------+---------------+-----------+------------+------------------+
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:您需要解决两个问题:
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).您人为地汇总了SELECT
子句中的内容以防止出现技术问题,但您可能需要更改此设置,具体取决于您的实际数据看起来如何(不是问题中显示的数据,而是实际数据)。 If you have answers with different references, then you might want to change your database structure to better reflect reality.如果您有不同参考的答案,那么您可能希望更改数据库结构以更好地反映现实。
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
.您没有告诉我们当ques_id
的值不是23 时会发生什么,所以我在ELSE
中添加了一个临时值。 You will need to change it to the one you actually need.您需要将其更改为您实际需要的那个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.