SQL Server 2008条件选择输出多个真值

时间:2012-08-09 19:32:14

标签: sql sql-server-2008

表:

Date = 20120101, ID = 123456, ParentID = 654321, System1 = 1, System2 = 1

用户可以在System1System2或两者中处于活动状态(1 = true,0 = false)。查询需要输出如下:

System1   System2
1         0            "System1", "All"
0         1            "System2", "All"
1         1            "System1", "System2", "All"
0         0            "All"

我正在尝试一个案例陈述,但我无法将其输出到我想要的地方。

2 个答案:

答案 0 :(得分:2)

select *,
         case when system1 = 1 and system2 = 0 then 'System1, All'
              when system1 = 1 and system2 = 1 then 'System1, System2, All'
              when system1 = 0 and system2 = 0 then 'All'
              when system1 = 0 and system2 = 1 then 'System2, All'
         end as output
from your_table

答案 1 :(得分:1)

我想到你想为数据中的每一行生成多行。

如果是这样,以下是:

select a.*
from ((select t.*, 'ALL' as thecode
       from t
      ) union all
      (select t.*, 'System1'
       from t
       where system1 = 1
      ) union all
      (select t.*, 'System2'
       from t
       where system2 = 1
      )
     ) a