bfuchs
asked on
SQL Query question
Hi Experts,
We have the following authorization table.
ID (PK)
PatientsID (Int)
EffectiveFrom (DateTime)
EffectiveTo (DateTime)
Now I would like to have a list of all gaps between authorizations.
Meaning if a patient had an auth 1/1/15 to 1/10/15 and another auth 1/11/15 to 1/30/15 and another one 1/31/15 to 2/15/15, that means there is no gap between authorizations, while if the last auth would be from 2/1/15 to 2/15/15 that would be considered a gap and record of last records should show up, including a number 1 which would be the count of days without auth (1/31/15).
We have the following authorization table.
ID (PK)
PatientsID (Int)
EffectiveFrom (DateTime)
EffectiveTo (DateTime)
Now I would like to have a list of all gaps between authorizations.
Meaning if a patient had an auth 1/1/15 to 1/10/15 and another auth 1/11/15 to 1/30/15 and another one 1/31/15 to 2/15/15, that means there is no gap between authorizations, while if the last auth would be from 2/1/15 to 2/15/15 that would be considered a gap and record of last records should show up, including a number 1 which would be the count of days without auth (1/31/15).
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK this already gave me some records, however before really testing I realized the following
1- It gives me records for patients that have only 1 auth.
2- also the one's without next from date I assume have no following auth should be omitted.
3- records that have no dates entered on EffectiveFrom or EffectiveTo are also showing up.
4- I Actually only need to see records of the last 3 months, where do I enter this condition?
1- It gives me records for patients that have only 1 auth.
2- also the one's without next from date I assume have no following auth should be omitted.
3- records that have no dates entered on EffectiveFrom or EffectiveTo are also showing up.
4- I Actually only need to see records of the last 3 months, where do I enter this condition?
Not sure what you exactly mean by #4, so you can change the following as necessary.
Modify the WHERE clause to:
WHERE (EffectiveFrom IS NOT NULL)
AND (EffectiveTo IS NOT NULL)
AND (NextFrom IS NOT NULL)
AND (EffectiveFrom >= DateAdd("m", -3, Date()))
AND (NextFrom-EffectiveTo) >=2
Modify the WHERE clause to:
WHERE (EffectiveFrom IS NOT NULL)
AND (EffectiveTo IS NOT NULL)
AND (NextFrom IS NOT NULL)
AND (EffectiveFrom >= DateAdd("m", -3, Date()))
AND (NextFrom-EffectiveTo) >=2
ASKER
@Dale,
I'm waiting for user to test, hopefully by tom will know if this works.
Thanks,
Ben
I'm waiting for user to test, hopefully by tom will know if this works.
Thanks,
Ben
ASKER
Thanks Dale this worked out well.
glad I could help.
SELECT ID, PatientsID, EffectiveFrom, EffectiveTo, NextFrom
FROM (
SELECT T1.ID, T1.PatientsID, T1.EffectiveFrom, T1.EffectiveTo
, Min(T2.EffectiveFrom) as NextFrom
FROM yourTable T1
LEFT JOIN yourTable T2
ON T1.PatientsID = T2.PatientsID
AND T1.EffectiveTo < T2.EffectiveFrom
) as sub
WHERE (NextFrom IS NULL)
OR (NextFrom - EffectiveTo >= 2)