Gap Between two two dates of different cells

RRO

I need help to create one script where i got stuck.

MemberId    BeginDate   EndDate Output  
1039725910  3/1/2014    8/10/2014   0   End on 10th August
1039725910  8/11/2014   11/10/2014  1   Start on 11th August, 1 day gap
1039725910  11/11/2014  12/31/2014  1   Start on 11th August, 1 day gap
1166164140  1/1/2014    4/30/2039   0   End on 30 April
1166164140  2/5/2014    12/30/2039  2   Start on 1st May, Here is a 2 days gap

Here For one member I have three different begin and end date. for the first records for each member, it would be 0, for the 2nd records, the gap would be (2nd Begindate - 1st EndDate). For 3rd record, The difference would be (3rd Begin date - 2nd EndDate) and so on...I am not able to attach any screenshot.

Kindly help me on this.

Regards, Ratan

jpw

You can use the row_number() window function together with a self-join to access the previous row partitioned by MemberId like this:

select 
    a.MemberId, 
    a.BeginDate, 
    a.EndDate,
    Output = ISNULL(DATEDIFF(DAY, isnull(b.EndDate, a.BeginDate), a.BeginDate), 0) 
from 
    (select *, rn = ROW_NUMBER() over (partition by memberid order by begindate) from members) a
left join 
    (select *, rn = ROW_NUMBER() over (partition by memberid order by begindate) from members) b
on a.MemberId = b.MemberId and a.rn - 1 = b.rn

With your sample data this would give you:

MemberId    BeginDate   EndDate     Output
1039725910  2014-03-01  2014-08-10  0
1039725910  2014-08-11  2014-11-10  1
1039725910  2014-11-11  2014-12-31  1
1166164140  2014-01-01  2039-04-30  0
1166164140  2014-05-02  2039-12-30  -9129

If you need to disregard the year component you'll have to do some date arithmetic.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Difference in years between two dates

분류에서Dev

how to display dates from two different tables?

분류에서Dev

Calculating the days between two given dates

분류에서Dev

sqlite : time difference between two dates in decimals

분류에서Dev

Room availability between two dates (beginner)

분류에서Dev

Difference between two dates in excel 2013

분류에서Dev

How to display all the dates between multiple two dates in a table?

분류에서Dev

Many-to-Many Relationship between two tables in two different databases

분류에서Dev

Toggle between two different vim configurations?

분류에서Dev

MySQL query to search in between two time range between two dates using timestamp data

분류에서Dev

Joda Time - Calculation of seconds between two dates throws an exception.

분류에서Dev

Elapsed days between two dates always giving one days difference

분류에서Dev

R - How to subset a table between two specific dates?

분류에서Dev

Mysql how to group by each day between two dates?

분류에서Dev

MDX: How to get the weeks between two dates in MDX Query

분류에서Dev

How many days between two dates are in a certain month?

분류에서Dev

SQL to get number of working days between two dates

분류에서Dev

How to get the number of weeks and its respective dates between two given dates (VB.NET)

분류에서Dev

What is the difference between these two type annotation and why they are different?

분류에서Dev

Accessing data between jar files with two different processes

분류에서Dev

Accessing data between jar files with two different processes

분류에서Dev

What is the difference between two different versions of VLC nightly builds?

분류에서Dev

Bitxor and Count the different rows values between two matrixes

분류에서Dev

Is this the best way to convert between two different types of array?

분류에서Dev

Share a Dagger 2 component between two different flavors

분류에서Dev

Syncing Thunderbird between two different (Arch) Linux Machines

분류에서Dev

Excel formula to search two cells for string and multiply corresponding numbers if string is different

분류에서Dev

How to Minus two dates in php

분류에서Dev

Addition of two dates on python 3

Related 관련 기사

  1. 1

    Difference in years between two dates

  2. 2

    how to display dates from two different tables?

  3. 3

    Calculating the days between two given dates

  4. 4

    sqlite : time difference between two dates in decimals

  5. 5

    Room availability between two dates (beginner)

  6. 6

    Difference between two dates in excel 2013

  7. 7

    How to display all the dates between multiple two dates in a table?

  8. 8

    Many-to-Many Relationship between two tables in two different databases

  9. 9

    Toggle between two different vim configurations?

  10. 10

    MySQL query to search in between two time range between two dates using timestamp data

  11. 11

    Joda Time - Calculation of seconds between two dates throws an exception.

  12. 12

    Elapsed days between two dates always giving one days difference

  13. 13

    R - How to subset a table between two specific dates?

  14. 14

    Mysql how to group by each day between two dates?

  15. 15

    MDX: How to get the weeks between two dates in MDX Query

  16. 16

    How many days between two dates are in a certain month?

  17. 17

    SQL to get number of working days between two dates

  18. 18

    How to get the number of weeks and its respective dates between two given dates (VB.NET)

  19. 19

    What is the difference between these two type annotation and why they are different?

  20. 20

    Accessing data between jar files with two different processes

  21. 21

    Accessing data between jar files with two different processes

  22. 22

    What is the difference between two different versions of VLC nightly builds?

  23. 23

    Bitxor and Count the different rows values between two matrixes

  24. 24

    Is this the best way to convert between two different types of array?

  25. 25

    Share a Dagger 2 component between two different flavors

  26. 26

    Syncing Thunderbird between two different (Arch) Linux Machines

  27. 27

    Excel formula to search two cells for string and multiply corresponding numbers if string is different

  28. 28

    How to Minus two dates in php

  29. 29

    Addition of two dates on python 3

뜨겁다태그

보관