Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Determine Days Since Last Visit

$
0
0

I think I may be overthinking this problem.

What I have is a table that consists of membership data including visit dates. For each visit date, I need to determine the number of days since the last visit.

I'm trying a combination of CTEs and partitions, but it is simply not coalescing in my head.

Following is a sample set of data;

KeyID          MemberID          AdmitDate          Last_Admit_Days
1                 2601                 2014-04-17         
2                 2001                 2014-05-04         
3                 1001                 2014-05-15         
4                 8001                 2014-05-28         
5                 2601                 2014-05-23         
6                 2601                 2014-05-28         
7                 9301                 2014-03-21         
8                 9301                 2014-05-15         
9                 9301                 2014-05-21         
10                8601                2014-05-26         
11                3601                2014-04-13         
12                3601                2014-05-05

I tried putting that in as a code block, but it ended up putting everything on one line.

Anyway, Member # 2601 has three admit dates; 2014-04-17, 2014-05-23 and 2014-05-28. So, the number in the Last_Admit_Days column for those records, should be the DATEDIFF in days between the AdmitDate and the AdmitDate of the immediately prior AdmitDate for #2601, like this;

KeyID          MemberID          AdmitDate          Last_Admit_Days
1                 2601                 2014-04-17          0
5                 2601                 2014-05-23          7
6                 2601                 2014-05-28          5

I am stumped. Maybe the weekend will refresh me to take a better stab at it, but it's confounding me at the moment and I thought I'd ask here to see if anyone had a quick idea off the top of their heads.

I truly appreciate any assistance.

Thanx!


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>