March 18, 2016 at 5:38 am
I don't know why I cannot figure this one out and I don't need anyone to write the procedure for me, but point me in a direction. We have a table with thousands of clients and are trying to determine lapses in coverage over 30 days. So we have the clientID with some information like name, etc. Then we have a startDate and an endDate. I need to return rows where there is 30+ days between the endDate and the beginning of the next startDate, if that makes sense. So if a client stops coverage at the end of a certain month, but then 60 days later starts coverage again I need to select that. So I am trying to do a datediff on that endDate in a prior row and compare it to the next instance of the startDate.
Thanks for any help anyone can give here
March 18, 2016 at 6:05 am
March 18, 2016 at 6:14 am
Thanks. It is 2008R2 (as to why I am posting here in SQL Server 2008). I did some searching online and have something actually running now so now I just need to clean it up a bit. What I did is something like this:
WITH rows AS
(
select d.*, ROW_NUMBER() over (Order by d.clientid, d.coverageplanname, d.StartDate) as rn
from
(
select
p.ClientId
,h.StartDate
,h.EndDate
,cp.CoveragePlanName
from ClientCoverageHistory h
join ClientCoveragePlans p on p.ClientCoveragePlanId = h.ClientCoveragePlanId
join CoveragePlans cp on p.CoveragePlanId = cp.CoveragePlanId
group by p.ClientId, cp.CoveragePlanName, h.StartDate, h.EndDate
) as d)
select
d.clientid
,d.CoveragePlanName
,d.StartDate
,d.EndDate
,DATEDIFF(day, d.EndDate , mp.StartDate) as dateLag
from rows d
join rows mp
on d.rn = mp.rn-1
March 18, 2016 at 6:44 am
Couple of minor tweaks:
WITH SequencedRows AS (
SELECT
p.ClientId,
h.StartDate,
h.EndDate,
cp.CoveragePlanName,
rn = ROW_NUMBER() OVER (PARTITION BY d.clientid, d.coverageplanname ORDER BY d.StartDate)
FROM ClientCoverageHistory h
INNER JOIN ClientCoveragePlans p
ON p.ClientCoveragePlanId = h.ClientCoveragePlanId
INNER JOIN CoveragePlans cp
ON p.CoveragePlanId = cp.CoveragePlanId
GROUP BY p.ClientId, cp.CoveragePlanName, h.StartDate, h.EndDate
)
SELECT
tr.clientid,
tr.CoveragePlanName,
tr.StartDate,
tr.EndDate,
dateLag = DATEDIFF(day, tr.EndDate, nr.StartDate)
FROM SequencedRows tr -- this row
LEFT JOIN SequencedRows nr -- next row
ON nr.ClientId = tr.ClientId
AND nr.CoveragePlanName = tr.CoveragePlanName
AND nr.rn = tr.rn + 1
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 18, 2016 at 7:01 am
Thanks for this! This does start me all in the direction I need to go.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply