Solved

how to find missing dates.

Posted on 2011-02-11
14
336 Views
Last Modified: 2012-06-27
I am trying to find missing dates.

I have a 1 single table with two columns:
ID (PK)
DateOfVisit (datetime)

 a Visit is schedule each month
Visit1 (Aug 2010)
Visit2 (Sep 2010)
Visit3 (Oct 2010)
Visit4 (Nov 2010)
Visit5 (Dec 2010)

I  need a query to I find the number of missing visit (s)?
for example number of missing visit= 2 when Visit2 (Sep 2010) and Visit5 (Dec 2010) are not entered.

Thank you for your help.
0
Comment
Question by:LougaLo
  • 7
  • 3
  • 2
  • +1
14 Comments
 
LVL 8

Expert Comment

by:raulggonzalez
ID: 34877494
Hi,

If you create a table with the schedule of visits, you can join it and count from your table visits to know when it's missing.


cheers

0
 
LVL 8

Expert Comment

by:raulggonzalez
ID: 34877902
More or less it would be like this,

You can modify it not to use MONTH() in the join if you have any index created over the column, or you can create a range in the table @schedule instead....

But I'm sure you get the point.

Good luck.


DECLARE @visits TABLE (ID INT IDENTITY,
						DateOfVisit datetime)

DECLARE @schedule TABLE (MonthVisit datetime)

INSERT INTO @visits VALUES ('2011-01-15')
INSERT INTO @visits VALUES ('2011-03-15')
INSERT INTO @visits VALUES ('2011-04-15')
INSERT INTO @visits VALUES ('2011-07-15')
INSERT INTO @visits VALUES ('2011-09-15')
INSERT INTO @visits VALUES ('2011-10-15')
INSERT INTO @visits VALUES ('2011-11-15')
INSERT INTO @visits VALUES ('2011-12-15')

INSERT INTO @schedule VALUES('2011-01-01')
INSERT INTO @schedule VALUES('2011-02-01')
INSERT INTO @schedule VALUES('2011-03-01')
INSERT INTO @schedule VALUES('2011-04-01')
INSERT INTO @schedule VALUES('2011-05-01')
INSERT INTO @schedule VALUES('2011-06-01')
INSERT INTO @schedule VALUES('2011-07-01')
INSERT INTO @schedule VALUES('2011-08-01')
INSERT INTO @schedule VALUES('2011-09-01')
INSERT INTO @schedule VALUES('2011-10-01')
INSERT INTO @schedule VALUES('2011-11-01')
INSERT INTO @schedule VALUES('2011-12-01')

SELECT DATENAME(month, S.MonthVisit), COUNT(V.ID)
FROM @schedule as S
LEFT JOIN @visits as V
	ON MONTH(S.MonthVisit) = MONTH(V.DateOfVisit)
GROUP BY S.MonthVisit

Open in new window

0
 

Author Comment

by:LougaLo
ID: 34878108
i already data entered and dates in the "TABLE".
attached is example table I have now my database
 tbDateOfVisit.xls
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:LougaLo
ID: 34878115
thank raulggonzalez: see my recent post
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34878160
;with cte as (select * ,row_number() over (partition by [id] order by [date] asc) as rn
                   from yourtable)
Select  a.*,'Missing '+convert(varchar(4),datediff(m,a.[date],coalesce(b.date,getdate())))
            ' Months Appointments until'+convert(varchar(12),coalesce(b.date,getdate()))
   from cte as a
   left outer join cte as B
     on a.id=b.id
   and b.rn=a.rn+1
 Where datediff(m,a.[date],coalesce(b.date,getdate()))>1
 order by a.id,a.rn desc
0
 

Author Comment

by:LougaLo
ID: 34878174
Lowfatspread:
just replaced yourtable with my table
here is the error msg I got
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '+'.

a.[date] (a. underlined in red)
a.* (a. underlined in red)
+ (+ underlined in red)
(b.date (b. underlined in red)
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 34878190
missing +?  can you post your version of the query if it fails again

;with cte as (select * ,row_number() over (partition by [id] order by [date] asc) as rn
                   from yourtable)
Select  a.*,'Missing '+convert(varchar(4),datediff(m,a.[date],coalesce(b.date,getdate())))
          + ' Months Appointments until'+convert(varchar(12),coalesce(b.date,getdate()))
   from cte as a
   left outer join cte as B
     on a.id=b.id
   and b.rn=a.rn+1
 Where datediff(m,a.[date],coalesce(b.date,getdate()))>1
 order by a.id,a.rn desc
0
 

Author Comment

by:LougaLo
ID: 34878309
Lowfatspread:
NO error where displayed but the output is not correct.
Let me do more investigations. for example
1. for ID 001 I have 7 records in my database table
2. the query output shows only 3 records.

0
 
LVL 22

Expert Comment

by:Thomasian
ID: 34878413
LougaLo,

What is the expected output for the given sample data?
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34879065
looks ok to me...

depends onn how you want to interpret it...

these both show the same thing...

but the second displays rows for the Missed Months...

"you haven't missed the current month, it isn't over yet..."

wether the gap between May 2010 and Jul 2010 should be reported as a 1 or 2 gaps
depends on the interpretation of "Month" and what we are supposed to be counting Visits or
gaps...

hth

;with cte as (select * ,row_number() over (partition by [id] order by [dateofvisit] asc) as rn
                   from yourtable)
Select  a.id
         ,CONVERT(varchar(12),a.dateofvisit) as LastVist
         ,convert(varchar(4),datediff(m,a.[dateofvisit],coalesce(b.dateofvisit,getdate())))
          + ' Months Appointments until '+convert(varchar(12),coalesce(b.dateofvisit,getdate()))
          as Missing
   from cte as a
   left outer join cte as B
     on a.id=b.id
   and b.rn=a.rn+1
 Where datediff(m,a.[dateofvisit],coalesce(b.dateofvisit,getdate()))>1
 order by a.id,a.rn desc
 
 
 ;with cte as (select * ,row_number() over (partition by [id] order by [dateofvisit] asc) as rn
                   from yourtable)
Select  a.id
     ,CONVERT(varchar(12),a.dateofvisit) as LastVist
     ,LEFT(datename(m,dateadd(m,v.number,a.dateofvisit)),3)
       +' '+convert(char(4),year(dateadd(m,v.number,a.dateofvisit))) as [missed Visit]
     ,v.number as "Missed # since last"
  from cte as a
  left outer join cte as b
    on a.id=b.id
   and b.rn =a.rn+1
   cross Join master.dbo.spt_values as v
   where convert(int,CONVERT(char(6),a.dateofvisit,112))
           <> CONVERT(int,convert(char(6),coalesce(b.dateofvisit,getdate()),112))-1
     and v.type='p'
     and v.number between 1 and 240
     and 
        convert(int,CONVERT(char(6),dateadd(m,v.number,a.dateofvisit),112)) <
        CONVERT(int,convert(char(6),coalesce(b.dateofvisit,getdate()),112))
           
 order by a.id,a.dateofvisit,v.number

Open in new window

0
 

Author Comment

by:LougaLo
ID: 34881057
Hi all,
attached is the file showing more detail and the expected output:

 tbVisit.xls
0
 

Author Comment

by:LougaLo
ID: 34881062
Lowfatspread:,
I would like to count the number of Visit missed. for example if 9 visits are scheduled and we have only 8 entries. the number of missed visit will be 1.
0
 
LVL 22

Expert Comment

by:Thomasian
ID: 34881080
Lowfatspread's 1st query should work fine. You just need to group to group the id and get the sum.

Here's the modified query, no points for me.

;with cte as (select * ,row_number() over (partition by [id] order by [dateofvisit] asc) as rn
                   from tblvisit)
Select  a.id
       ,SUM(datediff(m,a.[dateofvisit],coalesce(b.dateofvisit,getdate()))-1) as [Number of visits missed]
from cte as a inner join
     cte as B on a.id=b.id and b.rn=a.rn+1
Where datediff(m,a.[dateofvisit],coalesce(b.dateofvisit,getdate()))>1
group by a.id
order by a.id 

Open in new window

0
 

Author Comment

by:LougaLo
ID: 34881272
Thomasian,
You are correct.  Lowfatspread's first query works fine.

Now, I have another issue with my output.
I will discuss with my supervisor and will get back here or open a different question.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL 2008 R2 syntax 11 29
SQL Server 2012 r2 Make faster Temp Table 17 103
Problem when I run a simple storeproc - help 4 14
While in ##Table - Help 4 12
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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