0

I am currently creating SQl where I need to find DateDiff between two Dates.

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.
  2. Difference between RstartDate & REndDate should be more than year(365).

If All condition satisfy for each ItemId then I will Display Valid Else Not Valid.

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.

OutPut:

Expected Result :

ItemId Result

 101   NotValid
 102   Valid

But Getting following Result :

ItemId Result

 101    Valid   
 101    NotValid    
 102    Valid   

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.
  2. But I am not sure how to apply DateDiff for Rstartdate & RendDate. Since I need to Apply condition 2 for each row individually..

Note: For Row 2 the Difference btw RstartDate and REndDate is not more than 365.

Please help...

Community
  • 1
  • 1
RajGan
  • 801
  • 3
  • 13
  • 28
  • (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) Are the same so just use <-365 – PeterRing Jan 02 '14 at 15:39
  • 1
    Yes it should be > typo error..I have changed it and I have a lower limit where the date should be less than 30 days past from today and is no more than 1 year forward from today – RajGan Jan 02 '14 at 15:45
  • I don't see how you get vaild for itemid 102. Currentdate is the same on both and the DateDiff between that and today is 82. 82 is not between 30 and -365. Am I way off here? – SteveB Jan 02 '14 at 16:01
  • @user3083310 I am using UK Date Format DD/MM/YYYY.So the date diff would be 24 days.. – RajGan Jan 03 '14 at 13:49

1 Answers1

0

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. 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.

(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. If you are unsure what is failing you can change on 'QUOTED' to 'QUOTED1' and 'QUOTED2'.

To use betweeen I needed to make an inner case outputting 1 if failed, and confirm that the sum is 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
  • Yes you are correct I will change.. But need help to handle my second condition – RajGan Jan 02 '14 at 15:44
  • Daniel Thanks.. I dont want to use group by statement since my original query will have multi table joins.. But I will try to implement your solution and see if it works for me... – RajGan Jan 03 '14 at 13:39