I think you are close. I approach this sort of thing in steps. First, you are only interested in duplicate 'Imei' values. Your innermost query correctly finds the duplicate 'Imei' values:
Code:
SELECT Imei FROM tbl_Imei GROUP BY Imei HAVING COUNT(*) > 1
Next, let's construct a query which obtains the desired information you wish to report from your tables, but without regard to duplicates or JobID selection:
Code:
SELECT Customer, Phone_Make, Imei, Date, Job_Ref, Pallet
FROM Jobs INNER JOIN Phones ON Jobs.PhoneID=Phones.PhoneID
INNER JOIN Pallets ON Jobs.JobID=Pallets.JobID
INNER JOIN Imei ON Pallets.PalletID=Imei.PalletID
(Forgive me from stripping off the 'tbl_' prefix from your table names - it's something that for some reason Access folks like to add to their table names and it drives me nuts :))
I prefer to use the 'infix' or 'new' style JOIN syntax.
Now, if you added a WHERE clause to the above query which selected based on JobID you'd get your desired data, though still for everything for that job and not just the duplicate 'Imei' values.
Which should give you a hint.
If a table existed which contained the results of the duplicates query, then you would simply JOIN that table in with the others on the above query and that would give you what you want. In SQL Server, you can used what's called a
derived table, that is, I can directly include the duplicate-finding SELECT statement in the second query's FROM clause and treat it as though it were a table:
Code:
SELECT Customer, Phone_Make, Imei, Date, Job_Ref, Pallet
FROM Jobs INNER JOIN Phones ON Jobs.PhoneID=Phones.PhoneID
INNER JOIN Pallets ON Jobs.JobID=Pallets.JobID
INNER JOIN Imei ON Pallets.PalletID=Imei.PalletID
INNER JOIN
(SELECT Imei FROM tbl_Imei GROUP BY Imei HAVING COUNT(*) > 1) AS Dups
ON Dups.Imei=Imei.Imei
And this would give me the requested data, just for the duplicates. Add a WHERE clause selecting the desired JobID and then ORDER things appropriately, and you are done.
Unfortunately, I don't think you can use derived tables in Access. (Access is brain-dead). The above could also be done using a view instead of a derived table, and a view can be used in Access - that is, a stored query.
So, if you define another Access query which is the duplicate finding inner query, you can JOIN it as I have JOINed the derived table above.
Now, This query is certainly simpler than your original, but whether it is faster only trying it will tell...
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com