比案例陈述更有效

时间:2014-09-03 14:30:05

标签: sql-server tsql case

您好我有一个大规模的案例陈述,我想知道是否有比我使用的方式更好的方法。代码在我的数据上运行正常,只是想知道一种更容易生成或运行得更快的更好的方法。

    SELECT [FileName]
    ,[sheetname]
    ,CASE
    WHEN [FileName] + '_' + [SheetName] =  'VAL1' + '_' +  'VAL2' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL3' + '_' +  'VAL4' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL5' + '_' +  'VAL6' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL7' + '_' +  'VAL8' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL9' + '_' +  'VAL10' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL11' + '_' +  'VAL12' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL13' + '_' +  'VAL14' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL15' + '_' +  'VAL16' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL17' + '_' +  'VAL18' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL19' + '_' +  'VAL20' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL21' + '_' +  'VAL22' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL23' + '_' +  'VAL24' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL25' + '_' +  'VAL26' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL27' + '_' +  'VAL28' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL29' + '_' +  'VAL30' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL31' + '_' +  'VAL32' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL33' + '_' +  'VAL34' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL35' + '_' +  'VAL36' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL37' + '_' +  'VAL38' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL39' + '_' +  'VAL40' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL41' + '_' +  'VAL42' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL43' + '_' +  'VAL44' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL45' + '_' +  'VAL46' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL47' + '_' +  'VAL48' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL49' + '_' +  'VAL50' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL51' + '_' +  'VAL52' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL53' + '_' +  'VAL54' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL55' + '_' +  'VAL56' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL57' + '_' +  'VAL58' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL59' + '_' +  'VAL60' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL61' + '_' +  'VAL62' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL63' + '_' +  'VAL64' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL65' + '_' +  'VAL66' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL67' + '_' +  'VAL68' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL69' + '_' +  'VAL70' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL71' + '_' +  'VAL72' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL73' + '_' +  'VAL74' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL75' + '_' +  'VAL76' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL77' + '_' +  'VAL78' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL79' + '_' +  'VAL80' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL81' + '_' +  'VAL82' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL83' + '_' +  'VAL84' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL85' + '_' +  'VAL86' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL87' + '_' +  'VAL88' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL89' + '_' +  'VAL90' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL91' + '_' +  'VAL92' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL93' + '_' +  'VAL94' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL95' + '_' +  'VAL96' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL97' + '_' +  'VAL98' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL99' + '_' +  'VAL100' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL101' + '_' +  'VAL102' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL103' + '_' +  'VAL104' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL105' + '_' +  'VAL106' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL107' + '_' +  'VAL108' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL109' + '_' +  'VAL110' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL111' + '_' +  'VAL112' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL113' + '_' +  'VAL114' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL115' + '_' +  'VAL116' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL117' + '_' +  'VAL118' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL119' + '_' +  'VAL120' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL121' + '_' +  'VAL122' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL123' + '_' +  'VAL124' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL125' + '_' +  'VAL126' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL127' + '_' +  'VAL128' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL129' + '_' +  'VAL130' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL131' + '_' +  'VAL132' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL133' + '_' +  'VAL134' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL135' + '_' +  'VAL136' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL137' + '_' +  'VAL138' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL139' + '_' +  'VAL140' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL141' + '_' +  'VAL142' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL143' + '_' +  'VAL144' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL145' + '_' +  'VAL146' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL147' + '_' +  'VAL148' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL149' + '_' +  'VAL150' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL151' + '_' +  'VAL152' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL153' + '_' +  'VAL154' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL155' + '_' +  'VAL156' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL157' + '_' +  'VAL158' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL159' + '_' +  'VAL160' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL161' + '_' +  'VAL162' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL163' + '_' +  'VAL164' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL165' + '_' +  'VAL166' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL167' + '_' +  'VAL168' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL169' + '_' +  'VAL170' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL171' + '_' +  'VAL172' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL173' + '_' +  'VAL174' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL175' + '_' +  'VAL176' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL177' + '_' +  'VAL178' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL179' + '_' +  'VAL180' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL181' + '_' +  'VAL182' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL183' + '_' +  'VAL184' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL185' + '_' +  'VAL186' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL187' + '_' +  'VAL188' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL189' + '_' +  'VAL190' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL191' + '_' +  'VAL192' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL193' + '_' +  'VAL194' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL195' + '_' +  'VAL196' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL197' + '_' +  'VAL198' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL199' + '_' +  'VAL200' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL201' + '_' +  'VAL202' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL203' + '_' +  'VAL204' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL205' + '_' +  'VAL206' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL207' + '_' +  'VAL208' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL209' + '_' +  'VAL210' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL211' + '_' +  'VAL212' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL213' + '_' +  'VAL214' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL215' + '_' +  'VAL216' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL217' + '_' +  'VAL218' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL219' + '_' +  'VAL220' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL221' + '_' +  'VAL222' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL223' + '_' +  'VAL224' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL225' + '_' +  'VAL226' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL227' + '_' +  'VAL228' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL229' + '_' +  'VAL230' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL231' + '_' +  'VAL232' THEN [SHORTNAME]
    WHEN [FileName] + '_' + [SheetName] =  'VAL233' + '_' +  'VAL234' THEN [SHORTNAME]
    END AS SHORTNAME

    ,CASE
    WHEN  [SheetName] LIKE  '%SOME_TEXT1%' THEN 'SOME_MORE_TEXT1'
    WHEN  [SheetName] LIKE  '%SOME_TEXT2%' THEN 'SOME_MORE_TEXT2'
    WHEN  [SheetName] LIKE  '%SOME_TEXT3%' THEN 'SOME_MORE_TEXT3'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    WHEN  [SheetName] LIKE  '%SOME_TEXT%' THEN 'SOME_MORE_TEXT'
    END AS SOME_MORE_TEXT

    ,CASE
    WHEN [FileName] LIKE 'ATE_10426%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_12135%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_17251%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_2585X%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_33466%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_43788%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_44935%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_52607%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_56665%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_61659%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_68605%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_75543%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_78984%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_81633%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_89264%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_C5649%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_C6020%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_E2930%' THEN SUBSTRING([FileName],5,5)
    WHEN [FileName] LIKE 'ATE_G1255%' THEN SUBSTRING([FileName],5,5)
    END AS SUBFILENAME

    ,CASE
         WHEN [Branch] = 'L364X' THEN 'L364X'
    WHEN [Branch] = 'K4373' THEN 'K4373'
    WHEN [Branch] = 'AD015' THEN 'AD015'
    WHEN [Branch] = '38154' THEN '38154'
    WHEN [Branch] = '35669' THEN '35669'
    WHEN [Branch] = 'K4388' THEN 'K4388'
    WHEN [Branch] = 'A4360' THEN 'A4360'
    WHEN [Branch] = '8932X' THEN '8932X'
    WHEN [Branch] = 'E0530' THEN 'E0530'
    END AS Branch

    INTO T2
    FROM T1
抱歉,第一个案例陈述不对,它应该更像是这样,但对于相同数量的行,每行只有不同的数据

    SELECT   [FileName]
    ,[SheetName]
    ,CASE   WHEN [FileName] + '_' + [SheetName] = 'Adhoc Feed Data_Castaways ONEShop' THEN 'Castaw'
            WHEN [FileName] + '_' + [SheetName] = 'Adhoc Feed Data_Sol World Spar' THEN 'SolSpr'
            WHEN [FileName] + '_' + [SheetName] = 'Adhoc Feed Data_Sunway CoOp' THEN 'SunCO'

1 个答案:

答案 0 :(得分:1)

第一种情况与

相同
case when Left(FileName,3) = 'VAL' 
       And IsNumeric(SUbstring(FileName, 4,5)) = 1 
       And cast(SUbstring(FileName, 4,5) as integer) Between 1 and 123
       And Left(SheetName,3) = 'VAL' 
       And IsNumeric(SUbstring(SheetName, 4,5)) = 1 
       And cast(SUbstring(SheetName, 4,5) as integer) Between 1 and 123
     then Shortname end shortname

第二个,如果我理解你正在尝试正确做什么),则与

相同
Replace(FileName, 'SOME_TEXT', 'SOME_MORE_TEXT')

第三个可以替换为

case when Left(FileName, 4) = 'ATE_' 
        And substring(FileName,5,5) in ('10426', '12135', ... , 'G1255') 
     then substring(FileName,5,5) end as subFileName