ID dateandtime EmailID 73 6/8/2014 00:00:00 2 74 6/9/2014 00:00:00 2 75 6/10/2014 00:00:00 2 76 6/11/2014 00:00:00 2 77 6/12/2014 00:00:00 2 78 6/13/2014 00:00:00 2 79 6/14/2014 00:00:00 2 80 6/16/2014 00:00:00 2 81 6/17/2014 00:00:00 4 82 6/18/2014 00:00:00 4 83 6/19/2014 00:00:00 4 84 6/20/2014 00:00:00 4 89 6/27/2014 00:00:00 4 90 6/28/2014 00:00:00 4 91 6/29/2014 00:00:00 4 92 6/30/2014 00:00:00 4 93 6/1/2014 00:00:00 4 94 6/2/2014 00:00:00 4 95 6/3/2014 00:00:00 2 96 6/4/2014 00:00:00 2 97 6/5/2014 00:00:00 2 98 6/6/2014 00:00:00 2 99 6/7/2014 00:00:00 2 100 6/21/2014 00:00:00 4 101 6/22/2014 00:00:00 4 102 6/23/2014 00:00:00 4 103 6/24/2014 00:00:00 4 104 7/1/2014 00:00:00 4 105 7/2/2014 00:00:00 4 106 7/3/2014 00:00:00 4 121 7/6/2014 00:00:00 2 122 7/7/2014 00:00:00 2 123 7/8/2014 00:00:00 2
above record through i can generate below group of all date like as
StartDate - EndDate EmailID 6/1/2014 - 6/2/2014 4 6/3/2014 - 6/14/2014 2 6/16/2014 - 6/16/2014 2 6/17/2014 - 6/24/2014 4 6/27/2014 - 6/30/2014 4 7/1/2014 - 7/3/2014 4
i trying to find, for each EmailID, the contiguous blocks of dates? any one solve this question thanks in advance
This is an example of identifying groups of things that occur in order. In this case, the groups are in order by date and you want consecutive dates.
You can find the group by using the different in two sequential values. Enumerate the values by date. Then enumerate the values by date and
emailId
. The difference is a constant for sequential values with the sameemailId
. Here is how it would work in your case:As I understand you question you can use the following code:
select min([Start date - End date]) as StartDate, max([Start date - End date]) as EndSare, EmailID from dbo.T group by EmaiIID
This will do the trick for you. First identify the range starts and then the range ends and at the same time set a range identity. Last merge the range start and end sets.
The result is not as you might expect, however from what I can se correct.