Flag duplicate records in a query

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,152
I have a query where I need to flag if a Projects.ID has been duplicated due to a Notes.Notes field being added to the query.

Code:
SELECT Projects.ID, Max(Notes.ChangedDate) AS MaxOfChangedDate
FROM Notes RIGHT JOIN Projects ON Notes.PID = Projects.ID
WHERE (((Projects.ProjStatus)="In Progress"))
GROUP BY Projects.ID;

The sql above works fine to show the max date in the Notes.ChangedDate field; however, as soon as the field Notes, the query expands to show all notes as they are unique.

How can I add a field to the query which would annotate the Projects.ID field with a count like we would in Excel with countif?

In this field though, I would like the max date of the Notes.ChangedDate field to be the one and then any other duplicated records as 2, 3, 4, etc.

I could then filter the new field by 1 and get all the Projects.ID and their associated notes.
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Are you looking for something like this?
Code:
SELECT Projects.ID, Max(Notes.ChangedDate) AS MaxOfChangedDate[COLOR=#ff0000], Count(Notes.ChangedDate) AS CountOfNotes[/COLOR]
FROM Notes RIGHT JOIN Projects ON Notes.PID = Projects.ID
WHERE (((Projects.ProjStatus)="In Progress"))
GROUP BY Projects.ID;
 
Upvote 0
Hi Joe,

Actually, that just gives me the count to say there are 2 Projects.IDs.

Instead, I'm looking for the first record by the max date to show 1 and then second record would show 2 and if there was a third Projects.ID, it would show 3 and etc.
 
Upvote 0
Sorry for the poor explanation, I dont' use Access near as much as Excel. :)

Here are the results with using max date
G​
H​
1​
ID​
MaxOfChangedDate​
2​
9​
5/8/2017​
3​
12​
5/15/2017​
4​
13​
5/16/2017​
5​
19​
5/16/2017​
6​
20​
5/16/2017​
7​
21​
5/16/2017​
8​
40​
5/16/2017​
9​
42​
5/16/2017​
10​
43​
5/16/2017​
11​
45​
5/16/2017​
12​
46​
5/16/2017​
13​
48​
5/16/2017​
14​
49​
5/16/2017​
15​
50​
5/16/2017​

The problem comes in when I add the notes field which now make the max field miss the true max date based off of the ID as the notes makes everything unique.
A​
B​
C​
1​
ID​
ChangedDate​
Notes​
2​
9​
5/8/2017​
Notes​
3​
9​
1/18/2017​
Notes​
4​
12​
5/15/2017​
Notes​
5​
13​
5/16/2017​
Notes​
6​
19​
5/16/2017​
Notes​
7​
19​
2/13/2017​
Notes​
8​
20​
5/16/2017​
Notes​
9​
21​
5/16/2017​
Notes​
10​
40​
5/16/2017​
Notes​
11​
40​
2/13/2017​
Notes​
12​
42​
5/16/2017​
Notes​
13​
42​
2/13/2017​
Notes​
14​
43​
5/16/2017​
Notes​
15​
43​
2/13/2017​
Notes​
16​
45​
5/16/2017​
Notes​
17​
45​
2/13/2017​
Notes​
18​
46​
5/16/2017​
Notes​
19​
46​
2/13/2017​
Notes​
20​
48​
5/16/2017​
Notes​
21​
49​
5/16/2017​
Notes​
22​
49​
2/13/2017​
Notes​
23​
50​
5/16/2017​
Notes​
24​
50​
2/13/2017​
Notes​

This is what I would like to achieve thru the query
A​
B​
C​
D​
1​
ID​
ChangedDate​
Notes​
Cnt​
2​
9​
5/8/2017​
Notes​
1​
3​
9​
1/18/2017​
Notes​
2​
4​
12​
5/15/2017​
Notes​
1​
5​
13​
5/16/2017​
Notes​
1​
6​
19​
5/16/2017​
Notes​
1​
7​
19​
2/13/2017​
Notes​
2​
8​
20​
5/16/2017​
Notes​
1​
9​
21​
5/16/2017​
Notes​
1​
10​
40​
5/16/2017​
Notes​
1​
11​
40​
2/13/2017​
Notes​
2​
12​
42​
5/16/2017​
Notes​
1​
13​
42​
2/13/2017​
Notes​
2​
14​
43​
5/16/2017​
Notes​
1​
15​
43​
2/13/2017​
Notes​
2​
16​
45​
5/16/2017​
Notes​
1​
17​
45​
2/13/2017​
Notes​
2​
18​
46​
5/16/2017​
Notes​
1​
19​
46​
2/13/2017​
Notes​
2​
20​
48​
5/16/2017​
Notes​
1​
21​
49​
5/16/2017​
Notes​
1​
22​
49​
2/13/2017​
Notes​
2​
23​
50​
5/16/2017​
Notes​
1​
24​
50​
2/13/2017​
Notes​
2​

Now I can filter the query to the 1's and get the one max date with the associated note

Of course I'm using Excel to hopefully paint a better picture :)
 
Last edited:
Upvote 0
You cannot add the Notes field directly in to the Aggregate Query without Grouping on it, which would create those extra records, because uniqueness would then be determined on BOTH the ID and Notes field.

What you need to do start a new query, and add your original Aggregate Query you posted in your original post and your original Notes table, and join the two on BOTH the ID and DateChanged fields. Then you can return any fields from those two that you want to show.
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,351
Members
451,697
Latest member
pedroDH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top