Notiophilus
Registered User.
- Local time
- Today, 10:39
- Joined
- Jan 18, 2015
- Messages
- 42
I have two tables in a one-to-many relationship, tblBooks and tblBookAuthor. A book may have multiple authors, and several books may have the same title.
A book is considered a duplicate when both Title and AuthorID are the same (i.e. books 1 & 4 above, but not books 2 & 3).
I'm trying to make a query that will list duplicate books, but I can't make it work. The duplicates wizard only works on one table, so I can get duplicate titles OR duplicate authors, but never both. The only solution seems to be to make a second query on the first:
Is it possible to make this into a single query? I tried a subquery but my SQL's pretty shaky and I'm not sure I understand how GROUP BY works.
(The reason I'm using a query and not just a DCount / DLookup is that I want to use a recordset to retrieve the BookIDs of the duplicates, and DLookup will only return one ID.)
Code:
[FONT=Courier New][tblBook] [tblBookAuthor]
BookID (PK) Title [/FONT][FONT=Courier New][FONT=Courier New]BookID (PK) AuthorID (PK)[/FONT]
----------- -------------- [/FONT][FONT=Courier New][FONT=Courier New]------------ -------------[/FONT]
1 The Stranger [/FONT][FONT=Courier New][FONT=Courier New]1 11
[/FONT]2 Evolution 2 12
3 Evolution 2 13
4 The Stranger 3 14
4 11
[/FONT]
A book is considered a duplicate when both Title and AuthorID are the same (i.e. books 1 & 4 above, but not books 2 & 3).
I'm trying to make a query that will list duplicate books, but I can't make it work. The duplicates wizard only works on one table, so I can get duplicate titles OR duplicate authors, but never both. The only solution seems to be to make a second query on the first:
Code:
[qryDupeTitle]
SELECT tblBook.Title, tblBook.BookID, tblBookAuthor.AuthorID
FROM tblBook INNER JOIN tblBookAuthor ON tblBook.BookID = tblBookAuthor.BookID
WHERE (((tblBook.[Title]) In (SELECT [AuthorID] FROM [tblBook] As Tmp GROUP BY [Title] HAVING Count(*)>1 )))
ORDER BY tblBook.Title;
And
[qryDuplicates]
SELECT qryDupeTitle.AuthorID, qryDupeTitle.Title, qryDupeTitle.BookID
FROM qryDupeTitle
WHERE (((qryDupeTitle.AuthorID) In (SELECT [AuthorID] FROM [qryDupeTitle] As Tmp GROUP BY [AuthorID],[Title] HAVING Count(*)>1 And [Title] = [qryDupeTitle].[Title])))
ORDER BY qryDupeTitle.AuthorID, qryDupeTitle.Title;
Is it possible to make this into a single query? I tried a subquery but my SQL's pretty shaky and I'm not sure I understand how GROUP BY works.
(The reason I'm using a query and not just a DCount / DLookup is that I want to use a recordset to retrieve the BookIDs of the duplicates, and DLookup will only return one ID.)