Link to home
Start Free TrialLog in
Avatar of Jolynn Haney
Jolynn HaneyFlag for United States of America

asked on

Count number of days between end of one date range and start of next date range

I am stuck on a query that will calculate the number of days between the end of one date range and the start of the next date range for the same ID (IDs with multiple date ranges).  One ID maybe have one or more date ranges associated with it.

My data looks like this:

ID         Start_Date        End_Date    
1          2009-01-01        2009-03-31
1          2009-05-15        2009-06-30
2          2009-07-01        2009-08-31
2          2009-10-01        2009-12-15
2          2010-01-01        2010-03-31
3          2009-08-01        2009-09-30
4          2009-03-01        2009-03-31
4          2009-04-15        2009-05-31
5          2009-04-01        2009-04-30

I would like the output to add a "Days_Between" field and  look something like this

ID         Start_Date        End_Date      Days_Between  
1          2009-01-01        2009-03-31               0
1          2009-05-15        2009-06-30              45
2          2009-07-01        2009-08-31               0
2          2009-10-01        2009-12-15              30
2          2010-01-01        2010-03-31              16
3          2009-08-01        2009-09-30               0
4          2009-03-01        2009-03-31               0
4          2009-04-15        2009-05-31              15
5          2009-04-01        2009-04-30               0

Single IDs would be assigned 0 as would the first record in a series of like IDs.
Any assistance is appreciated.
Thank you.
ASKER CERTIFIED SOLUTION
Avatar of LCSandman8301
LCSandman8301
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
something like this?
select *, case when rn <> 1 then datediff(d, start_date, end_date) else 0 end as days_between from 
(
select *, row_number() over (partition by ID order by start_date) rn from yourtable
) a

Open in new window

Avatar of aqif_g
aqif_g

1          2009-01-01        2009-03-31               0
1          2009-05-15        2009-06-30              45

For me, your concept of id's is totally wrong.

ID is a key to identify a single record uniquely.

First, user keys like,
ID         Start_Date        End_Date      Days_Between  
1          2009-01-01        2009-03-31               0
2          2009-05-15        2009-06-30              45
3          2009-07-01        2009-08-31               0
4          2009-10-01        2009-12-15              30
5          2010-01-01        2010-03-31              16
6          2009-08-01        2009-09-30               0
7          2009-03-01        2009-03-31               0
8          2009-04-15        2009-05-31              15
9          2009-04-01        2009-04-30               0

Now,

to handle dates, there are two ways to handle them.

1 - at mySQL level ( in queries )
2 - at Programming level

I usually handle dates at programming level.


You can pick any two consecutive  data entries like,

(I will use PHP way of declaring variables.)

$id = 3; (or any value)

$nxt = $id + 1;

select * from table where id >= $id AND id<=$nxt;

Now,

two records will gets selected. 1 - with id = $id and other will nbe with id = ($id + 1), that is very next record.

Now,

Subtract End date date of second record date from Start date of first one. What you will get by subtracting (end date of 1st from start date of 2nd record)?

It totally depends upon your knowledge of programming language and how you perform date manipulation.

I  hate handling dates. I usually save dates as STRING hen in PHP I read date (string) from database then I use strtodate() to convert it into int date that is count of seconds from 1971(i guess, donot know exact count). Means thats a big integer value in seconds.

To perform this task in PHP,

1- Before saving new Entry. I would have checked if any previous entry in DB exists. if YES, then read that entry date.
2- convert that entry into integers using strtodate()
3- subtract that date from current entry date. (This will gives me time difference in seconds and I can change it into hours by diving by 3600 and into days by diving by (3600 * 24 ))
4- Then sving the results ;)



regards,
aqif



Avatar of Jolynn Haney

ASKER

lsandman8301 - your solution was what I was looking for. Thank you for your assistance!

ralmada - your solution returned the number of day between the start and end of the date range on each row,  Maybe I applied your solution incorrectly.

agif_g - I am not a programmer so your solution was not helpful. Also my example field name ID was poorly chosen - that is not, of course, the real field name.  Sorry if that distracted you.

Thank you for your responses.  
glad i could help
This is what you posterd
1          2009-01-01        2009-03-31               0
1          2009-05-15        2009-06-30              45
from 2009-01-01 to 2009-06-30 there are no 45 days. the 45 days is for the second row. That's why I've posted that solution. You should be more specific when posting.
 
"a query that will calculate the number of days between the end of one date range and the start of the next date range for the same ID (IDs with multiple date ranges).  One ID maybe have one or more date ranges associated with it."

I thought I was specific in the narrative description of my problem (see above). Perhaps you could provide a suggestion on how I might of been clearer so I will not make this mistake again.  Thank you for your response.
Like I said if you put an example it should match what you are writing. It's common sense only.