如何为表中的每列指定1或0?

时间:2015-11-12 15:48:38

标签: sql sql-server

我有一个名为s1.Split(',').Zip(s2.Split(','), (name, num) => new Info{Name = name, Num = num}) 的表,有15列。根据{{​​1}},将使用某些列,并且不会使用某些列。例如,Person为5将使用列EventIdEventIdName。同时,Address 10将使用列PhoneEventId

我想我必须用Name创建一个表,并以某种方式放入1或0来知道特定事件应该具有哪些列。但是,我不知道如何将所有这些结合起来并使其发挥作用。

我正在使用SQL Server。

1 个答案:

答案 0 :(得分:0)

create table  #test (EventID varchar(max), Name varchar(max), [Address] varchar(max), [Phone] varchar(max))
insert into #test 
values ('1','Abc','gabanes adfga','1521623612'),
       ('3','adz','gabwega abae' ,'1521623612'),
       ('5','qqb','asdhahn ansxeh','1521623612'),
       ('5','qas','smystrj sthshs','1521623612'),
       ('2','qar','sthtr sthsht ','1521623612'),
       ('1','qha','msgnsn abawe ','1521623612')

; with cte as 
(
select *, case when EventID = 5 then 1 else 0 end as Flag from #test 
) 
select Name, [Address], Phone  from cte
where flag = 1

;with cte1
 as ( select *, case when EventID = 5 then 1 else 0 end as Flag from #test )
 select Name, Phone from cte1 
 where flag != 1 

我已经随机命名了列,只是将垃圾数据放入字段中。但是,希望这有助于实现您的目标。