The following query will display all Dewey Decimal numbers that have been duplicated in the ‘book’ table:
SELECT dewey_number, COUNT(dewey_number) AS NumOccurrences FROM book GROUP BY dewey_number HAVING ( COUNT(dewey_number) > 1 )
However, what I’d like to do is have my query display the name of the authors associated with the duplicated entry (the ‘book’ table and ‘author’ table are connected by ‘author_id’). In other words, the query above would yield the following:
dewey_number | NumOccurrences ------------------------------ 5000 | 2 9090 | 3
What I’d like the results to display is something similar to the following:
author_last_name | dewey_number | NumOccurrences ------------------------------------------------- Smith | 5000 | 2 Jones | 5000 | 2 Jackson | 9090 | 3 Johnson | 9090 | 3 Jeffers | 9090 | 3
Any help you can provide is greatly appreciated. And, in case it comes into play, I’m using a Postgresql DB.
UPDATE: Please note that ‘author_last_name’ is not in the ‘book’ table.
A nested query can do the job.
(I don’t know if this is the fastest way to achieve what you want.)
Update: Here is my data
And here is the result of the above query: