Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Query question

Posted on 2015-02-24
8
Medium Priority
?
106 Views
Last Modified: 2015-02-26
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).
0
Comment
Question by:bfuchs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 48

Expert Comment

by:Dale Fye
ID: 40629853
If you are using Access for this, try the following.  Because of the non-equijoin, you will only be able to edit this in the SQL view.  This should also work in SQL Server, but I'm guessing that there are more efficient ways to do it in SQL Server.

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)
0
 
LVL 4

Author Comment

by:bfuchs
ID: 40629869
Hi,
See attached.
Untitled.png
0
 
LVL 48

Accepted Solution

by:
Dale Fye earned 2000 total points
ID: 40629876
Oops, forgot the Group By clause in the subquery:

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
GROUP BY T1.ID, T1.PatientsID, T1.EffectiveFrom, T1.EffectiveTo
) as sub
WHERE (NextFrom IS NULL)
OR (NextFrom - EffectiveTo >= 2)
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 4

Author Comment

by:bfuchs
ID: 40629890
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?
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 40630284
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
0
 
LVL 4

Author Comment

by:bfuchs
ID: 40632003
@Dale,

I'm waiting for user to test, hopefully by tom will know if this works.

Thanks,
Ben
0
 
LVL 4

Author Closing Comment

by:bfuchs
ID: 40634246
Thanks Dale this worked out well.
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 40634311
glad I could help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question