SQL中的Case语句和OR

时间:2021-08-01 22:25:28

Can someone please advise on the below? I have a number of fields which I would like to use and combine these to get the data in one column. I am using the following case statement but am not getting the results I expect.

有人可以在下面提出建议吗?我有许多我想使用的字段,并将这些字段组合在一起以获取数据。我正在使用以下案例陈述,但我没有得到我期望的结果。

CASE
WHEN m.u_hearing = 1 THEN 'Hearing'
WHEN m.u_learning_reading_diff = 1 THEN 'Learning or reading Difficulty'
WHEN m.u_long_term_ill = 1 THEN 'Long Term Illness'
WHEN m.u_mental_illness = 1 THEN 'Mental Illness'
WHEN m.u_mobility = 1 THEN 'Mobility'
WHEN m.u_physical_coordination = 1 THEN 'Physical Co-ordination'
WHEN m.u_physical_dis = 1 THEN 'Physical Disability'
WHEN m.u_red_physical_cap = 1 THEN 'Reduced_physical_capacity'
WHEN m.u_speech = 1 THEN 'Speech'
WHEN m.u_vision = 1 THEN 'Vision'
WHEN m.u_other_dis = 1 THEN 'Other_Disability'
WHEN (m.u_hearing = 1 AND (m.u_learning_reading_diff = 1 OR (m.u_speech = 1))) THEN 'Multiple'

It is the last statement that is not giving the result I would like as if there are multiple fields with Yes then I would like multiple returned but it seems it is picking the first case.

这是最后一个声明没有给出我想要的结果,好像有多个字段是,然后我想要多个返回,但它似乎是选择第一个案例。

3 个解决方案

#1


1  

Put the "hardest" condition first

把“最难”的条件放在第一位

case WHEN m.u_hearing = 1 AND (m.u_learning_reading_diff = 1 OR m.u_speech = 1) THEN 'Multiple' 
     WHEN m.u_hearing = 1 THEN 'Hearing' ...

Because a case stops at the first condition that is true

因为案例在第一个条件下停止是真的

#2


0  

When using CASE statements, they will function in the order you have specified.

使用CASE语句时,它们将按您指定的顺序运行。

So if m.u_Hearing = 1 then it will stop at the first line and not reach the bottom one.

因此,如果m.u_Hearing = 1,则它将在第一行停止而不到达底部。

This will work for what you require, however list it in the order you prefer.

这将适用于您所需的内容,但请按您喜欢的顺序列出。

CASE
WHEN (m.u_hearing = 1 AND (m.u_learning_reading_diff = 1 OR (m.u_speech = 1))) THEN 'Multiple'
WHEN m.u_hearing = 1 THEN 'Hearing'
WHEN m.u_learning_reading_diff = 1 THEN 'Learning or reading Difficulty'
WHEN m.u_long_term_ill = 1 THEN 'Long Term Illness'
WHEN m.u_mental_illness = 1 THEN 'Mental Illness'
WHEN m.u_mobility = 1 THEN 'Mobility'
WHEN m.u_physical_coordination = 1 THEN 'Physical Co-ordination'
WHEN m.u_physical_dis = 1 THEN 'Physical Disability'
WHEN m.u_red_physical_cap = 1 THEN 'Reduced_physical_capacity'
WHEN m.u_speech = 1 THEN 'Speech'
WHEN m.u_vision = 1 THEN 'Vision'
WHEN m.u_other_dis = 1 THEN 'Other_Disability'

#3


0  

From what I know you cannot use your first statement again in another "when" like your

据我所知,你不能再在另一个“何时”使用你的第一个陈述

WHEN m.u_hearing = 1 THEN 'Hearing'

Above condition try to use else if it is working and feasible for you in end.

以上条件尝试使用其他,如果它最终工作和可行。

Else 'Multiple'

#1


1  

Put the "hardest" condition first

把“最难”的条件放在第一位

case WHEN m.u_hearing = 1 AND (m.u_learning_reading_diff = 1 OR m.u_speech = 1) THEN 'Multiple' 
     WHEN m.u_hearing = 1 THEN 'Hearing' ...

Because a case stops at the first condition that is true

因为案例在第一个条件下停止是真的

#2


0  

When using CASE statements, they will function in the order you have specified.

使用CASE语句时,它们将按您指定的顺序运行。

So if m.u_Hearing = 1 then it will stop at the first line and not reach the bottom one.

因此,如果m.u_Hearing = 1,则它将在第一行停止而不到达底部。

This will work for what you require, however list it in the order you prefer.

这将适用于您所需的内容,但请按您喜欢的顺序列出。

CASE
WHEN (m.u_hearing = 1 AND (m.u_learning_reading_diff = 1 OR (m.u_speech = 1))) THEN 'Multiple'
WHEN m.u_hearing = 1 THEN 'Hearing'
WHEN m.u_learning_reading_diff = 1 THEN 'Learning or reading Difficulty'
WHEN m.u_long_term_ill = 1 THEN 'Long Term Illness'
WHEN m.u_mental_illness = 1 THEN 'Mental Illness'
WHEN m.u_mobility = 1 THEN 'Mobility'
WHEN m.u_physical_coordination = 1 THEN 'Physical Co-ordination'
WHEN m.u_physical_dis = 1 THEN 'Physical Disability'
WHEN m.u_red_physical_cap = 1 THEN 'Reduced_physical_capacity'
WHEN m.u_speech = 1 THEN 'Speech'
WHEN m.u_vision = 1 THEN 'Vision'
WHEN m.u_other_dis = 1 THEN 'Other_Disability'

#3


0  

From what I know you cannot use your first statement again in another "when" like your

据我所知,你不能再在另一个“何时”使用你的第一个陈述

WHEN m.u_hearing = 1 THEN 'Hearing'

Above condition try to use else if it is working and feasible for you in end.

以上条件尝试使用其他,如果它最终工作和可行。

Else 'Multiple'