多个SQL Case语句

时间:2018-04-02 06:35:39

标签: sql sql-server sql-server-2012

请帮助我编写脚本以选择以下格式的记录(附上图像)。只有标志存储在数据库中。其他列应根据标志选择进行硬编码。我已经为它创建了一个脚本及其工作,但我想知道是否有更好的方法来做到这一点。这应该在SQL Server 2012中完成

结果:

enter image description here

DECLARE @temp AS TABLE (Flag1 INT,Flag2 INT,Flag3 INT,Flag4 INT,Flag5 INT,Name VARCHAR(10))
INSERT INTO @temp VALUES (1,0,0,0,0,'aaa'),(0,1,0,0,0,'bbb'),(0,0,1,0,0,'ccc'),(0,0,0,1,0,'ddd'),(1,0,0,0,1,'eee'),
(1,0,0,1,0,'fff'),(0,1,0,1,0,'ggg'),(0,0,1,0,0,'hhh'),(0,0,0,0,1,'iii'),(0,0,0,0,1,'jjj')

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,
        CASE WHEN flag1=1 THEN 'Id1_col1_item1' END col1 , 
        CASE WHEN flag1=1 THEN 'Id1_col2_item1' END col2, 
        CASE WHEN flag1=1 THEN 'Id1_col3_item1' END col3, 
        CASE WHEN flag1=1 THEN 'Id1_col4_item1' END col4 FROM @temp

        UNION ALL

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,
        CASE WHEN flag2=1 THEN 'Id2_col1_item1' END col1 , 
        CASE WHEN flag2=1 THEN 'Id2_col2_item1' END col2, 
        CASE WHEN flag2=1 THEN 'Id2_col3_item1' END col3, 
        CASE WHEN flag2=1 THEN 'Id2_col4_item1' END col4 FROM @temp

        UNION ALL 

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,
        CASE WHEN flag2=1 THEN 'Id2_col1_item1' END col1 , 
        CASE WHEN flag2=1 THEN 'Id2_col2_item1' END col2, 
        CASE WHEN flag2=1 THEN 'Id2_col3_item2' END col3,           
        CASE WHEN flag2=1 THEN 'Id2_col4_item2' END col4 FROM @temp

        UNION ALL 

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,
        CASE WHEN flag2=1 THEN 'Id2_col1_item1' END col1 , 
        CASE WHEN flag2=1 THEN 'Id2_col2_item1' END col2, 
        CASE WHEN flag2=1 THEN 'Id2_col3_item3' END col3,
        CASE WHEN flag2=1 THEN 'Id2_col4_item3' END col4 FROM @temp

        UNION ALL 

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,
        CASE WHEN flag2=1 THEN 'Id2_col1_item1' END col1 , 
        CASE WHEN flag2=1 THEN 'Id2_col2_item1' END col2, 
        CASE WHEN flag2=1 THEN 'Id2_col3_item4' END col3,
        CASE WHEN flag2=1 THEN 'Id2_col4_item4' END col4 FROM @temp

        UNION ALL 

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,
        CASE WHEN flag3=1 THEN 'Id3_col1_item1' END col1 , 
        CASE WHEN flag3=1 THEN 'Id3_col2_item1' END col2, 
        CASE WHEN flag3=1 THEN 'Id3_col3_item1' END col3,
        CASE WHEN flag3=1 THEN 'Id3_col4_item1' END col4 FROM @temp

        UNION ALL 

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,CASE WHEN flag3=1 THEN 'Id3_col1_item1' END col1 , 
        CASE WHEN flag3=1 THEN 'Id3_col2_item1' END col2, 
        CASE WHEN flag3=1 THEN 'Id3_col3_item2' END col3,
        CASE WHEN flag3=1 THEN 'Id3_col4_item2' END col4 FROM @temp

        UNION ALL 

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,
        CASE WHEN flag3=1 THEN 'Id3_col1_item1' END col1 , 
        CASE WHEN flag3=1 THEN 'Id3_col2_item1' END col2, 
        CASE WHEN flag3=1 THEN 'Id3_col3_item3' END col3,
        CASE WHEN flag3=1 THEN 'Id3_col4_item3' END col4 FROM @temp

        UNION ALL 

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,
        CASE WHEN flag3=1 THEN 'Id3_col1_item1' END col1 , 
        CASE WHEN flag3=1 THEN 'Id3_col2_item1' END col2, 
        CASE WHEN flag3=1 THEN 'Id3_col3_item4' END col3,
        CASE WHEN flag3=1 THEN 'Id3_col4_item4' END col4 FROM @temp

        UNION ALL 

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,
        CASE WHEN flag3=1 THEN 'Id4_col1_item1' END col1 , 
        CASE WHEN flag3=1 THEN 'Id4_col2_item1' END col2, 
        CASE WHEN flag3=1 THEN 'Id4_col3_item1' END col3,
        CASE WHEN flag3=1 THEN 'Id4_col4_item1' END col4 FROM @temp

        UNION ALL 

SELECT  Flag1,Flag2,Flag3,Flag4,Flag5,
        CASE WHEN flag3=1 THEN 'Id5_col1_item1' END col1 , 
        CASE WHEN flag3=1 THEN 'Id5_col2_item1' END col2, 
        CASE WHEN flag3=1 THEN 'Id5_col3_item1' END col3,
        CASE WHEN flag3=1 THEN 'Id5_col4_item1' END col4 FROM @temp

先谢谢

1 个答案:

答案 0 :(得分:0)

  

我已将其修改为sql server,试试这个......

 SELECT isnull(Flag1,'') Flag1, isnull(Flag2,'') Flag2, isnull(Flag3,'')
 Flag3, isnull(Flag4,'') Flag4, isnull(Flag5,'') Flag5, 
 (CASE WHEN flag1=1 THEN 'Id1_col1_item1' ELSE 
 CASE WHEN flag2=1 THEN 'Id2_col1_item1' ELSE  
 CASE WHEN flag3=1 THEN 'Id3_col1_item1' 
 ELSE  '' 
 END) AS col1,
 (CASE WHEN flag1=1 THEN 'Id1_col2_item1' ELSE 
 CASE WHEN flag2=1 THEN 'Id2_col2_item1' ELSE  
 CASE WHEN flag3=1 THEN 'Id3_col2_item1' 
 ELSE  '' 
 END) AS col2,
 ..........................
 ..........................
 FROM @temp;