Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Floyd Bird
Starting Member
2 Posts |
Posted - 2009-01-17 : 13:36:48
|
| I have a query that I want to use to find breaks in coverage for a medicare participant. This query uses only one table, does a self-join, and uses a sub query. The way it is written now,it will show full-year periods that HAVE coverage. What I want is to show the number of days where there is no coverage. The only one in the result set should be from 1/1/2004 to 3/1/2004. Here is the SQL: CREATE TABLE Medicare(Client varchar(30), StartDate datetime, EndDate datetime) INSERT INTO Medicare VALUES ("Murphy","2001-01-01", "2001-12-31")INSERT INTO Medicare VALUES ("Murphy","2002-01-01", "2002-12-31")INSERT INTO Medicare VALUES ("Murphy","2003-01-01", "2003-12-31")INSERT INTO Medicare VALUES ("Murphy","2004-03-01", "2004-12-31")INSERT INTO Medicare VALUES ("Murphy","2005-01-01", "2005-12-31")INSERT INTO Medicare VALUES ("Murphy","2006-01-01", "2006-12-31") SELECT DISTINCT m1.Client, m1.EndDate As m1END, m2.StartDate AS m2START, DATEDIFF("d",m1.EndDate,m2.Startdate) AS LapseDaysFROM Medicare AS m1 INNER JOIN Medicare m2 ON (m1.Client = m2.Client)WHERE ( DATEADD("d",1,m1.EndDate) = (SELECT MAX(m3.StartDate) FROM Medicare m3 WHERE (m3.Client = m1.Client) AND DATEADD("d",1,m3.StartDate) <= DATEADD("d",-1,m2.EndDate))) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-17 : 13:56:14
|
| [code]SELECT DATEADD(dd,1,tmp.EndDate),t.StartDateFROM Table tCROSS APPLY(SELECT TOP 1 EndDate FROM Table WHERE Client=t.Client AND EndDate<t.StartDate ORDER BY EndDate DESC) tmpWHERE EndDate<> DATEADD(dd,-1,StartDate)[/code] |
 |
|
|
Floyd Bird
Starting Member
2 Posts |
Posted - 2009-01-17 : 14:06:36
|
| Thank you! :) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-17 : 15:49:14
|
[code]DECLARE @Medicare TABLE ( Client varchar(30), StartDate datetime, EndDate datetime )INSERT @MedicareSELECT 'Visakh', '2003-01-01', '2003-09-30' UNION ALLSELECT 'Visakh', '2003-10-15', '2004-12-31' UNION ALLSELECT 'Visakh', '2004-12-21', '2006-12-31' UNION ALLSELECT 'Peso', '2003-01-01', '2003-09-30' UNION ALLSELECT 'Peso', '2003-09-15', '2006-12-31' UNION ALLSELECT 'Murphy', '2001-01-01', '2001-12-31' UNION ALLSELECT 'Murphy', '2002-01-01', '2002-12-31' UNION ALLSELECT 'Murphy', '2003-01-01', '2003-12-31' UNION ALLSELECT 'Murphy', '2004-03-01', '2004-12-31' UNION ALLSELECT 'Murphy', '2005-01-01', '2005-12-31' UNION ALLSELECT 'Murphy', '2006-01-01', '2006-12-31'-- PesoSELECT m.Client, 1 + DATEDIFF(DAY, MIN(m.StartDate), MAX(m.EndDate)) AS totDays, COUNT(DISTINCT DATEADD(DAY, v.Number, m.StartDate)) AS uniqueCovered, COUNT(v.Number) AS totCoveredFROM @Medicare AS mINNER JOIN master..spt_values AS v ON v.Type = 'P' AND v.Number <= DATEDIFF(DAY, m.StartDate, m.EndDate)GROUP BY m.Client-- Visakh16SELECT t.Client, DATEADD(DAY, 1, tmp.EndDate), t.StartDateFROM @Medicare AS tCROSS APPLY ( SELECT TOP 1 x.EndDate FROM @Medicare AS x WHERE x.Client = t.Client AND x.EndDate < t.StartDate ORDER BY x.EndDate DESC ) AS tmpWHERE tmp.EndDate <> DATEADD(DAY, -1, t.StartDate)[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|
|
|
|
|