Skip to content
Advertisement

Query to Return Records with Consecutive Dates by Group in Access 2016

I have a table with a date and an ID field. An extract is set out below. I would like to run a query to return all records where the same ID appears on consecutive dates. There may be no consecutive dates or two or more consecutive dates.

Here is the extract:

Date No_ID
09/06/2021 24694000
09/06/2021 20102886
09/06/2021 12873514
09/06/2021 21307300
09/06/2021 13310606
10/06/2021 24694000
10/06/2021 14590427
10/06/2021 23520905
10/06/2021 7424945
10/06/2021 11437404
10/06/2021 12873514

Desired output:

Date No_ID
09/06/2021 24694000
09/06/2021 12873514
10/06/2021 24694000
10/06/2021 12873514

Advertisement

Answer

Consider:

SELECT Table1.Date, Table1.No_ID
FROM Table1 INNER JOIN (SELECT [Date], [Date]-1 AS PrevDate, No_ID FROM Table1)  AS Dup ON (Table1.No_ID = Dup.No_ID) AND (Table1.Date = Dup.[PrevDate])
UNION SELECT Table1.Date, Table1.No_ID
FROM Table1 INNER JOIN (SELECT [Date], [Date]+1 AS NextDate, No_ID FROM Table1)  AS Dup ON (Table1.No_ID = Dup.No_ID) AND (Table1.Date = Dup.[NextDate]);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement