怎么样的情况如果那么问题?

时间:2013-05-23 13:29:48

标签: sql-server-2008-r2

我正在尝试选择属于这些条件的行

    IF kid=IND and kid=Family 
       then select Kid=IND 

    If kid only has Family 
       then select family. 

    if kid only has IND 
       then select IND.      

我将如何使用CASE进行此操作?

离。

kid_id   date       type

001      1/1/2013   IND
001      1/1/2013   Family
002      1/2/2013   Family
003      1/3/2013   IND

结果

001   1/1/2013   IND
002   1/2/2013   Family
003   1/3/2013   IND 

1 个答案:

答案 0 :(得分:0)

select 
case 
When (kid=ind and kid=family) Then ind
When kid=family then family
When kid=ind then ind
else
  null
end

From SomeTable

鉴于孩子,ind和家庭是表中的列。

你的意思是这样吗

select kid_id,date, 
case 
When (kid=ind and kid=family) Then 'Both'
When kid=family then family
When kid=ind then ind
else
  'None'
end as TypeGroup

From SomeTable