I was sitting several hours for trying to solve on specific problem but I couldn’t get it. Finally, I decided to post it here. Here are some records,
╔════════╦════════════╦═════════╦══════════════════╗
║ AUTOID ║ PERSONNAME ║ FLOWER ║ Other columns... ║
╠════════╬════════════╬═════════╬══════════════════╣
║ 1 ║ Alex ║ Rose ║ ║
║ 2 ║ Rene ║ Rose ║ ║
║ 3 ║ Rene ║ Jasmin ║ ║
║ 4 ║ Darwin ║ Rose ║ ║
║ 5 ║ Alex ║ Rose ║ ║
║ 6 ║ Darwin ║ Jasmin ║ ║
║ 7 ║ Alex ║ Rose ║ ║
║ 8 ║ Rene ║ Jasmin ║ ║
║ 9 ║ Hello ║ World ║ ║
║ 10 ║ Hello ║ World ║ ║
╚════════╩════════════╩═════════╩══════════════════╝
How can I get this result, the person who has only one type of flower on all his/her records.
╔════════════╦════════════╗
║ PERSONNAME ║ FLOWERNAME ║
╠════════════╬════════════╣
║ Alex ║ Rose ║
║ Hello ║ World ║
╚════════════╩════════════╝
the best one I tried was this query below, and still not working.
SELECT DISTINCT t1.PersonName, t1.Flower
FROM TableName t1
INNER JOIN
(
SELECT personname, COUNT(DISTINCT flower) totals
FROM tableName
GROUP BY personname, Flower
) t2 ON t1.personname = t2.personname and
t2.totals = 1
Any Idea?
you can use
GROUP BYclause,HAVINGclause andCOUNT()on this problem, no need to join on a subquery