MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 6th, 2012, 11:31 AM
ojnabieoot ojnabieoot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 1 ojnabieoot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m
Reputation Power: 0
Selecting entries by date which occur nearby

My problem is the following: I need to select records with the same name that have dates which occur within five days of each other. For example, if I had

Tim 5/3/06
Tim 5/5/06
Tim 11/2/06
Bill 1/13/07
Bill 1/20/07
Judy 7/4/06
Judy 7/7/06

my procedure should return

Tim 5/3/06
Tim 5/5/04
Judy 7/4/06
Judy 7/7/06

Any suggestions for doing this? I assume my best bet would be a cursor, though I'm unfamiliar with how to use a cursor to return this sort of data.

Here's the (woefully insufficient) query I came up with:

SELECT *

FROM tablename
and personname in (

SELECT personname

FROM tablename
GROUP BY personname HAVING DATEDIFF(day, min(date), max(date)) < 6 and DATEDIFF(day, min(date), max(date)) > 0
)

The problem is that this doesn't return the right answers when there are multiple dates associated with each person - for example, it would return Judy, but not Tim.

Reply With Quote
  #2  
Old April 26th, 2012, 06:10 AM
haritvs haritvs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 1 haritvs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m
Reputation Power: 0
select p1.*
from prob01 as p1, prob01 as p2
where p1.name = p2.name
and p1.sampledate <> p2.sampledate
and datediff( dd, p1.sampledate, p2.sampledate) < 5
and datediff( dd, p2.sampledate, p1.sampledate) < 5

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Selecting entries by date which occur nearby

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap