Querying between records with date ranges within a certain time

JJMAT84@GMAIL.COM

New member
Local time
Today, 04:56
Joined
Nov 5, 2012
Messages
1
I have not found the solution to this looking elsewhere, but basically what I am trying to do is the following:

person date
1 1/1/2012
1 12/15/2012
2 6/12/2012
1 6/13/2012
2 6/14/2012
3 6/16/2012

I want a query which shows the count or records for which a person has a date within 30 days of each other. This is being queried from a large table in which records would may not be sequential by person. Thus in my example above, the third record for person 1 would be ommitted as well as the record for person 3. Any help would be greatly appreciated. Thank you!
 
You might try something like the following (substitute highlighted text with actual table/field names):
Code:
SELECT COUNT(*) AS NUM
FROM [B][I]MyTable[/I][/B] AS T1
WHERE EXISTS (
    SELECT T2.*
    FROM [B][I]MyTable[/I][/B] AS T2
    WHERE T2.[B][I]person[/I][/B] = T1.[B][I]person[/I][/B]
    AND (Abs(T2.[B][I]date[/I][/B] - T1.[B][I]date[/I][/B]) Between 1 And 30));
 

Users who are viewing this thread

Back
Top Bottom