Find Duplicates with a Twist

koolwaters

Active Member
Joined
May 16, 2007
Messages
403
I am trying to create a query that finds duplicates in a particular column but only if the date column is on or after the current date.

I used the Wizard which correctly returns the duplicated records. However, when I add my date criteria I am not getting the results I want.

For example, let's say I am trying to find a duplicated CustomerJobID but only if the ServiceDate is scheduled for today or later. So I have two JobIDs of 23. The find duplicates query returns those two records. I then need to filter those two records if the ServiceDate is on or after the current date. I created a query based on the find duplicates and for the ServiceDate field I have the criteria >=Date(). This then returns one record because only one job is scheduled for today or after. This is not what I want however. Since only one job meets the criteria, it is no longer a duplicate job.

Is there any way to accomplish this without creating multiple find duplicates queries?

Thanks in advance for any help.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Can you post the SQL code of your query attempt, after you add the Date Criteria, and we'll see if we can help you clean it up to do what you want?
 
Upvote 0
Code:
SELECT tblCustomerJob.[CustomerJobID], tblCustomerJob.[AccountNo], tblCustomerJob.[ServiceDate]
FROM tblCustomerJob
WHERE (((tblCustomerJob.[CustomerJobID]) In (SELECT [CustomerJobID] FROM [tblCustomerJob] As Tmp GROUP BY [CustomerJobID] HAVING Count(*)>1 )) AND ((tblCustomerJob.[ServiceDate])>=Date()))
ORDER BY tblCustomerJob.[CustomerJobID];

The above SQL statement returns all jobs with a duplicate CustomerJobID where the ServiceDate is on or after the current date. Using just the duplicate CustomerJobID there may be multiple records, however, after the records are filtered by the date criteria, there may only be one duplicate job ID. In those instance, it is no longer a duplicate job and I would not want that record. So essentially, after the query is executed I only want data if there is 2 or more records.
 
Upvote 0
Rather than attempt to include totals within a find duplicates query (not that I think you can) I'd create a second (totals) query using the first as the table and set the criteria on a Count field to be > 1.
 
Upvote 0
Here is some SQL code that will do it all in a single shot,
Code:
SELECT Tab.CustomerJobID, Tab.AccountNo,Tab.ServiceDate
FROM tblCustomerJob as Tab
INNER JOIN
(SELECT tblCustomerJob.CustomerJobID, Count(tblCustomerJob.AccountNo) AS CountOfAccountNo
FROM tblCustomerJob
WHERE tblCustomerJob.ServiceDate>=Date()
GROUP BY tblCustomerJob.CustomerJobID
HAVING Count(tblCustomerJob.AccountNo)>1) as Tot
ON Tab.CustomerJobID=Tot.CustomerJobID
WHERE Tab.ServiceDate>=Date();
Though, it may be easier to understand to do it in two queries.

Going that route, you would use the first query to return all future records that have more than one record (for a particular JobID):
Code:
SELECT tblCustomerJob.CustomerJobID, Count(tblCustomerJob.AccountNo) AS CountOfAccountNo
FROM tblCustomerJob
WHERE tblCustomerJob.ServiceDate>=Date()
GROUP BY tblCustomerJob.CustomerJobID
HAVING Count(tblCustomerJob.AccountNo)>1;
and then join that query back to your original table like this:
Code:
SELECT tblCustomerJob.CustomerJobID, tblCustomerJob.AccountNo, tblCustomerJob.ServiceDate
FROM tblCustomerJob INNER JOIN Query1 ON tblCustomerJob.CustomerJobID = Query1.CustomerJobID
WHERE tblCustomerJob.ServiceDate>=Date();
Both will get you to the same point.

[/CODE]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,805
Messages
6,162,081
Members
451,738
Latest member
gaseremad

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