如果贝娄条件为真,我需要从THEN
列获取Mortgage_Type
的值。
使用Item_No加入表。我需要找到Mortgage_Type 每个物品。如果Category是main和Equipment,我有20+ Mortgage_Types 为null然后应显示相关的Mortgage_Type
when SUM (Mortgage_Type)is not null and SUM (Equipment)is null and
sum(Category) ='M' THEN “value from Mortgage_Type column”
答案 0 :(得分:1)
只需输入列名:
CASE
WHEN (Mortgage_Type)is not null and (Equipment)is null and (Category) ='M'
THEN Mortgage_Type
ELSE <other value>
END
答案 1 :(得分:0)
你可以这样做:
CASE WHEN Col1 is not null AND Col2 is null AND Col3 = 'M' THEN 'value'
...
ELSE 0 END as Column
或
CASE Col1 WHEN <value> THEN '' WHEN <value> THEN '' ELSE <your value>END
答案 2 :(得分:0)
如果要为类别的每个值输出不同的名称,则应使用SQL case语句,例如:
*
CASE
WHEN cat=1 THEN 'category one'
WHEN cat=2 THEN 'category two'
WHEN cat=3 THEN 'category tree'
ELSE 'other category'
END
*
您可以在表格中准备一个带有连接的简单SQL语句,如下所述:
select A.Mortgage_Type,B.Item_No from table_A A, table_B B where A.Item_No = B.Item_No
and A.Mortgage_Type is not null and B.Equipment is null and A.Category ='M'
我真的不明白你的“B.equipment is null”条件,是强制性的吗? 如果您没有任何Morgage_type,您想提供默认值吗?
答案 3 :(得分:0)
如果您的mortgage_type列不在加入中使用的表中,那么
- 使用此
CASE
WHEN (Mortgage_Type)is not null and (Equipment)is null and (Category) ='M'
THEN (SELECT Mortgage_Type FROM <table_name> WHERE <condition>)
ELSE <........>
END
如果在连接中存在mortgate_type column't表,那么
- 使用此
CASE
WHEN (Mortgage_Type)is not null and (Equipment)is null and (Category) ='M'
THEN mortgage_type
ELSE <........>
END
答案 4 :(得分:0)
假设我们的两个表是Mortgage_details(Item_No,Mortgage_type,category),而Equipment_Details(Item_No,Equipment)假设您拥有列的数据类型。
为了选择所需的Mortgage_Type,您可以在下面的查询中包含一个case语句:
select Item_No,
case when Mortgage_Type is not null and Equipment is null and Category ='M'
THEN Mortgage_Type
Else Mortgage_Type
End as Relevent_Mortgage_type,
from Mortgage_details m
join Equipment_details e
on m.Item_no = e.Item_no;
而不是Mortgage_Type in Else对于不满足上述条件的行,您可以拥有任何值。