简体   繁体   English

SQL无法找到DateDiff

[英]SQL unable to find DateDiff

I am currently creating SQl where I need to find DateDiff between two Dates. 我目前正在创建SQl,需要在两个日期之间找到DateDiff。

My sample Table 我的样品表

Sno ItemId   LineItemId CurrentEndDate     RStartDate      REndDate

1   101      541        10/12/2013          11/12/2013  10/12/2014

2   101      542        10/01/2014          10/01/2014  12/12/2014 

3   101      543        09/01/2014          11/01/2014  10/01/2016 

4   102      544        10/12/2013          11/12/2013  10/12/2014 

5   102      589        10/12/2013          11/12/2013  10/12/2014 

I have following conditions: 我有以下条件:

  1. CurrentEndDate should be btw Today-30 AND Today+365. CurrentEndDate应该是Today-30和Today + 365。
  2. Difference between RstartDate & REndDate should be more than year(365). RstartDate和REndDate之间的差异应大于year(365)。

If All condition satisfy for each ItemId then I will Display Valid Else Not Valid. 如果所有条件都满足每个ItemId,则我将显示“有效”,否则“无效”。

Note: All the Rows should satisfy the above conditions. 注意:所有行应满足上述条件。

I wrote a SQL and unable to find a proper way in getting DateDiff for RstartDate & REndDate. 我写了一条SQL,找不到为RstartDate和REndDate获取DateDiff的正确方法。

OutPut: 输出:

Expected Result : 预期结果 :

ItemId Result ItemId结果

 101   NotValid
 102   Valid

But Getting following Result : 但是得到以下结果:

ItemId Result ItemId结果

 101    Valid   
 101    NotValid    
 102    Valid   

SQL 的SQL

 Select ItemId,
    Case:
          (Cast(DATEDIFF(DAY,MIN(CurrentEndDate) OVER(PARTITION BY ITemId),GETDATE()) AS int) < 30 
        AND Cast(DATEDIFF(DAY,MIN(CurrentEndDate) OVER(PARTITION BY ITemId),GETDATE()) AS int) > -365)
    AND (Cast(DATEDIFF(DAY,MAX(CurrentEndDate) OVER(PARTITION BY ITemId),GETDATE()) AS int) < 30 
                              AND Cast(DATEDIFF(DAY,MAX(CurrentEndDate) OVER(PARTITION BY ITemId),GETDATE()) AS int) > -365)
    AND CAST(DATEDIFF(DAY,RStartDate,REndDate) + 1 AS int) >= 365
THEN 'QUOTED'
                            else 'Not Valid'
End  As Result
    From ItemTable

Explanation: 说明:

  1. For CurrentEndDate I Found Max & Min for each ItemId and Applied the condition 1. Thinking if Max and Min satisfy the Rest of the in between rows would also satisfy. 对于CurrentEndDate,我为每个ItemId找到了Max&Min,并应用了条件1.考虑Max和Min是否满足其余行之间的其余部分。
  2. But I am not sure how to apply DateDiff for Rstartdate & RendDate. 但是我不确定如何将DateDiff应用于Rstartdate和RendDate。 Since I need to Apply condition 2 for each row individually.. 由于我需要分别为每行应用条件2。

Note: For Row 2 the Difference btw RstartDate and REndDate is not more than 365. 注意:对于第2行,RstartDate和REndDate的差btw不大于365。

Please help... 请帮忙...

There is a problem with your rules. 您的规则有问题。

Look more detailed at this. 看起来更详细。


        (Cast(DATEDIFF(DAY,MIN(CurrentEndDate) OVER(PARTITION BY ITemId),GETDATE()) AS int) < 30 
        AND Cast(DATEDIFF(DAY,MIN(CurrentEndDate) OVER(PARTITION BY ITemId),GETDATE()) AS int) < -365)

If the last row is fase the row above will also be false. 如果最后一行是fase,则上面的行也将为false。 If one result is false if it is less then -365 it will also be less then 30. Maybe use between 0 and 30 and between -365 and 0. Or maybe then use between -365 and 30 and skip one. 如果一个结果为假,并且小于-365,那么它也小于30。也许在0到30之间以及-365和0之间使用。或者在-365到30之间使用并跳过一个。

(Cast(DATEDIFF(DAY,MIN(CurrentEndDate) OVER(PARTITION BY ITemId),GETDATE()) AS int) between -365 and 30)

I changed the code using group by as you, as I can see it in you request, just wants one output per itemid, so this might work. 我按照您的要求使用group by更改了代码,正如您在请求中看到的那样,我只希望每个itemid一个输出,所以这可能有用。 If you are unsure what is failing you can change on 'QUOTED' to 'QUOTED1' and 'QUOTED2'. 如果不确定发生了什么,可以将“ QUOTED”更改为“ QUOTED1”和“ QUOTED2”。

To use betweeen I needed to make an inner case outputting 1 if failed, and confirm that the sum is 0. 要使用betweeen,我需要制作一个内箱,如果失败,则输出1,并确认总和为0。


Select 
    ItemId,
    Case 
        when sum(
                    case when DATEDIFF(DAY,CurrentEndDate, GETDATE())  between -365 and 30 then 0 else 1 end
                ) = 0 THEN 'QUOTED'
        when min(DATEDIFF(DAY,RStartDate,REndDate)) > 365 THEN 'QUOTED'
        else 'Not Valid'
    End  As Result
    From ItemTable
    group by ItemId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM