1

我正在为这个程序而苦苦挣扎。

如果是周一到周四,我希望​​一切都提前 90 天;

SELECT 
        PR.ClientID
        ,PR.NewDealEndDate
    INTO 
      #OriginalRenewalDetails
    FROM  
        Shiva.dbo.ProductRemortgage PR WITH (NOLOCK) 
        LEFT JOIN Shiva.dbo.ClientLead AS CL WITH (NOLOCK) 
        ON CONVERT(VARCHAR(50), CL.OriginatorReferenceID) = CONVERT(VARCHAR(50), PR.ClientID)
    WHERE -- 90 days from now. 
        PR.NewDealEndDate = DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))            
        AND CL.ClientID IS NULL

但是,如果是星期天,我希望在 Where 子句中进行以下更改。这样我也能得到周五和周六的结果。

WHERE -- 90 days from now. 
    PR.NewDealEndDate BETWEEN DATEADD(dd, 88, DATEDIFF(dd, 00, GETDATE())) AND DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
    AND CL.ClientID IS NULL

我找不到这样做的有效方法。任何帮助表示赞赏。

4

4 回答 4

2

使用IF.

伪:

if day = sunday
begin
   select with 88
end
else 
begin
   select with 90
end

或将 WHERE 子句更改为:

WHERE (PR.NewDealEndDate = DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
       AND CL.ClientID IS NULL AND Datepart(weekday, getdate()) <> 1)
      OR
      (PR.NewDealEndDate BETWEEN DATEADD(dd, 88, DATEDIFF(dd, 00, GETDATE())) 
       AND DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
       AND CL.ClientID IS NULL
       AND Datepart(weekday, getdate()) = 1)
于 2012-05-21T14:26:04.430 回答
1

您可以执行以下操作:

SELECT PR.ClientID
        , PR.NewDealEndDate
INTO #OriginalRenewalDetails
FROM Shiva.dbo.ProductRemortgage PR WITH (NOLOCK) 
LEFT JOIN Shiva.dbo.ClientLead AS CL WITH (NOLOCK) 
    ON CONVERT(VARCHAR(50), CL.OriginatorReferenceID) = CONVERT(VARCHAR(50), PR.ClientID)
WHERE CL.ClientID IS NULL
AND
(
    (
        PR.NewDealEndDate = DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
        AND  Datepart(weekday, getdate()) <> 1 -- 1 is Sunday
    )
    OR
    (
        PR.NewDealEndDate BETWEEN DATEADD(dd, 88, DATEDIFF(dd, 00, GETDATE())) 
        AND DATEADD(dd, 90, DATEDIFF(dd, 00, GETDATE()))
        AND Datepart(dayofweek, getdate()) = 1
    )
)
于 2012-05-21T14:35:30.100 回答
1
declare @adjustment int--SELECT DATEPART(weekday, getdate()) 
set @adjustment =
CASE 
    WHEN DATEPART(weekday, getdate()) 
    in (1,2,3,4,5) THEN 90     
ELSE 88
END

WHERE  
    PR.NewDealEndDate BETWEEN DATEADD(dd, @adjustment...
于 2012-05-21T14:41:16.127 回答
1

这里不需要动态 SQL 或两个单独的查询。**DAY = SUNDAY**用您当前用于确定星期天的任何逻辑替换该部分:

WHERE PR.NewDealEndDate BETWEEN DATEADD(DAY, 
    CASE WHEN **DAY = SUNDAY** THEN 88 ELSE 90 END, 
    DATEDIFF(DAY, 0, GETDATE())) AND DATEADD(DAY, 90, DATEDIFF(dd, 0, GETDATE()))
AND CL.ClientID IS NULL;

您还可以事先计算范围,例如

DECLARE @start SMALLDATETIME, @end SMALLDATETIME;

SET @end = DATEADD(DAY, 90, DATEDIFF(DAY, 0, GETDATE()));
SET @start = CASE WHEN **DAY = SUNDAY** THEN DATEADD(DAY, -2, @end) ELSE @end END;

WHERE PR.NewDealEndDate BETWEEN @start END @end
AND CL.ClientID IS NULL;
于 2012-05-21T14:45:27.500 回答