如何消除重复的子查询?

时间:2011-02-19 06:33:25

标签: sql-server tsql

如何消除重复的子查询?

SDPDDJ - (SELECT IBOPV FROM TESTDTA.F4102 WHERE IBLITM = SDLITM and IBMCU = SDMCU )

来自以下查询:

Declare @OPV int
Declare @Today_JD int
Declare @DayOfWeek int
SET  @Today_JD =  dbo.date2jde (convert(varchar(10),getdate(),111)) --today in Julian   
SET  @DayOfWeek = DATEPART(dw, dbo.jde2date(@Today_JD))    -- Day of week (1 to 7)

UPDATE TESTDTA.F4211  SET SDVR03 = 1
FROM TESTDTA.F4211

WHERE sdkcoo = 30001 and sdsrp1 = 'ITO' and sdsobk > 0 and  sdvend > '' and 
      sddoco = 2606544 and sdlnid = 27000 and 

    CASE 
        WHEN ((SDPDDJ - (SELECT IBOPV FROM TESTDTA.F4102 WHERE IBLITM = SDLITM and IBMCU = SDMCU ) >= @Today_JD) AND (@DayOfWeek = 2) and (SDVEND= 1010 OR SDVEND=4010)) THEN 1
            WHEN ((SDPDDJ - (SELECT IBOPV FROM TESTDTA.F4102 WHERE IBLITM = SDLITM and IBMCU = SDMCU ) >= @Today_JD) AND (@DayOfWeek = 3) and (SDVEND= 110051 OR SDVEND=110052 OR SDVEND = 2010)) THEN 1            
            WHEN ((SDPDDJ - (SELECT IBOPV FROM TESTDTA.F4102 WHERE IBLITM = SDLITM and IBMCU = SDMCU ) >= @Today_JD) AND (@DayOfWeek = 4) and (SDVEND= 50001))   THEN 1
            WHEN ((SDPDDJ - (SELECT IBOPV FROM TESTDTA.F4102 WHERE IBLITM = SDLITM and IBMCU = SDMCU ) >= @Today_JD) AND (@DayOfWeek = 5) and (SDVEND= 110034))  THEN 1    
        ELSE 0      
    END  = 1

2 个答案:

答案 0 :(得分:1)

简单地说,将它从CASE子句中删除并直接放入WHERE子句中。如果不是这样,则CASE语句的所有分支都变为FALSE - >它以CASE ELSE结束 - > 0与最后的= 1不匹配。所以请把它放在案例之外。

Declare @OPV int
Declare @Today_JD int
Declare @DayOfWeek int
SET  @Today_JD =  dbo.date2jde (convert(varchar(10),getdate(),111)) --today in Julian   
SET  @DayOfWeek = DATEPART(dw, dbo.jde2date(@Today_JD))    -- Day of week (1 to 7)

UPDATE TESTDTA.F4211  SET SDVR03 = 1
FROM TESTDTA.F4211
WHERE sdkcoo = 30001 and sdsrp1 = 'ITO' and sdsobk > 0 and  sdvend > '' and 
      sddoco = 2606544 and sdlnid = 27000 and 
      (SDPDDJ - (SELECT IBOPV FROM TESTDTA.F4102 WHERE IBLITM = SDLITM and IBMCU = SDMCU ) >= @Today_JD)
AND
    CASE 
        WHEN (@DayOfWeek = 2) and SDVEND IN (1010,4010) THEN 1
        WHEN (@DayOfWeek = 3) and SDVEND IN (110051,110052,2010) THEN 1            
        WHEN (@DayOfWeek = 4) and SDVEND= 50001)   THEN 1
        WHEN (@DayOfWeek = 5) and SDVEND= 110034)  THEN 1    
        ELSE 0      
    END  = 1

我会更进一步完全放弃案例陈述

Declare @OPV int
Declare @Today_JD int
Declare @DayOfWeek int
SET  @Today_JD =  dbo.date2jde (convert(varchar(10),getdate(),111)) --today in Julian   
SET  @DayOfWeek = DATEPART(dw, dbo.jde2date(@Today_JD))    -- Day of week (1 to 7)

UPDATE TESTDTA.F4211  SET SDVR03 = 1
FROM TESTDTA.F4211
WHERE sdkcoo = 30001 and sdsrp1 = 'ITO' and sdsobk > 0 and  sdvend > '' and 
      sddoco = 2606544 and sdlnid = 27000 and 
      ((SDPDDJ - (SELECT IBOPV FROM TESTDTA.F4102 WHERE IBLITM = SDLITM and IBMCU = SDMCU ) >= @Today_JD)
AND (
    ((@DayOfWeek = 2) and SDVEND IN (1010,4010)) OR
    ((@DayOfWeek = 3) and SDVEND IN (110051,110052,2010)) OR
    ((@DayOfWeek = 4) and (SDVEND= 50001)) OR
    ((@DayOfWeek = 5) and (SDVEND= 110034))
    )

答案 1 :(得分:0)

您可以使用SQL Server update ... from语法,例如:

UPDATE  t1
SET     SDVR03 = 1
FROM    TESTDTA.F4211 t1
JOIN    TESTDTA.F4102 t2
ON      IBLITM = SDLITM and IBMCU = SDMCU
WHERE   ...