查询许多CASE语句 - 优化

时间:2010-12-24 14:06:24

标签: sql sql-server tsql query-optimization case-statement

  

可能重复:
  Query with many CASE statements - optimization

大家好,

我有一个非常脏的查询,确保可以优化,因为它有很多CASE语句!

SELECT (CASE pa.KplusTable_Id WHEN 1 THEN sp.sp_id 

    WHEN 2 THEN fw.fw_id
 WHEN 3 THEN s.sw_Id
 WHEN 4 THEN id.ia_id END) as Deal_Id,  
max(CASE pa.KplusTable_Id WHEN 1 THEN sp.Trans_Id 
              WHEN 2 THEN fw.Trans_Id
                WHEN 3 THEN s.Trans_Id
              WHEN 4 THEN id.Trans_Id END) as TransId_CurrentMax
INTO #MaxRazlicitOdNull
FROM #PotencijalniAktuelni pa LEFT JOIN kplus_sp sp (nolock) on sp.sp_id=pa.Deal_Id     AND pa.KplusTable_Id=1
      LEFT JOIN kplus_fw fw (nolock) on fw.fw_id=pa.Deal_Id AND pa.KplusTable_Id=2  
         LEFT JOIN dev_sw s (nolock) on s.sw_Id=pa.Deal_Id AND pa.KplusTable_Id=3
           LEFT JOIN kplus_ia id (nolock) on id.ia_id=pa.Deal_Id AND pa.KplusTable_Id=4
 WHERE isnull(CASE pa.KplusTable_Id WHEN 1 THEN sp.BROJ_TIKETA 
                       WHEN 2 THEN fw.BROJ_TIKETA
              WHEN 3 THEN s.tiket
              WHEN 4 THEN id.BROJ_TIKETA END, '')<>'' 
GROUP BY CASE pa.KplusTable_Id WHEN 1 THEN sp.sp_id 
             WHEN 2 THEN fw.fw_id
       WHEN 3 THEN s.sw_Id
       WHEN 4 THEN id.ia_id END

因为我有几次相同的条件,你是否知道如何优化查询,使其更简单和更好。欢迎所有建议!

提前TnX!

维迪奇

0 个答案:

没有答案