Here's a problem perhaps you have had. You want to select unique email addresses, first names and last names out of a database for a newsletter or to sell them a new mortgage or whatever. Being the nice guy that you are you don't want to send them multiple messages, so you want to eliminate duplicates, right? the problem is that SELECT DISTINCT... doesn't always work in this instance. For example, John Doe put his information in as John Doe in one case and John H. Doe in another. Selecting distinct for name and email will give you a duplicate name with the same email. Now obviously you could solve this problem in your Coldfusion code - but wouldn't it be nice to fix up the query?
This is exactly the problem we ran into recently. We had people signing up for "seminars" for a client and we would end up with duplications from folks who had signed up more than once. The fix? Join the table to itself, group by and use HAVING - like so:
Thanks - nice tip on the inline view. I think I have a blog post on that somewhere as well. As for making the userID unique... it IS unique. The problem is legacy data where the first and lastname may NOT be unique resulting in what I guess I should call "psuedo" duplicates - where the user is actually the same person, but "some" of the data doesn't match.
SELECT
MAX(su.userid) AS userid,
MAX(su.fname) AS fname,
MAX(su.seminarid) AS id,
su.email
FROM seminarusers AS su
GROUP BY su.email
Sure, you may get a mix-and-match of first names and last names (Adam Zellar + Betsy Ross = Betsy Zellar) ... but does it matter? You're not going to have two people with two different last names sign up with the same email address. Right?