Solved

how to find missing dates.

Posted on 2011-02-11
14
331 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now