2

桌子:

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

用户可以在 、 或两者中处于活动状态System1System21 = 真,0 = 假)。查询需要输出如下:

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

我正在尝试一个案例陈述,但我无法让它输出我想要的方式。

4

2 回答 2

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
于 2012-08-09T19:35:51.577 回答
1

I'm getting the idea that you want to generate multiple rows for each row in your data.

If so, the following does this:

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
于 2012-08-09T19:42:52.363 回答