I'm guessing that your accountID is unique, probably the primary key of your accounts table. If that's true, then in your example 'j w account' is an accountname which occurs 6 times, presumably with 6 different accountid's. What would your desired result then be?
Would you be looking for:
accountID accountname sum
1234123414 j w account 6
9876543210 j w account 6
...
which doesn't seem terribly useful ... but what do I know...
But let's work on the subquery idea. Your original query returns the set of duplicate accountnames and a count. The results of a query can also be thought of as a table; in fact, in SQL this is called a 'derived table'. We can then JOIN this derived table with your original accounts table and display columns from each:
Code:
SELECT accountID, dups.accountname , dupcount
FROM accounts
INNER JOIN (SELECT accountname, count(*) as dupcount FROM accounts
GROUP BY accountname HAVING count(*) > 1) as dups
ON accounts.accountname = dups.accountname;
There's a little housekeeping to be done. The derived table must be given a name in the FROM clause, and we'll need to give a name to the COUNT expression so we can refer to it.
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com