i am trying to create sql procedure or function that need to find duplicate users in my users table (duplicate in case the users have the same email). I want to save the users in a new table like this:
id | user_id | duplicate_users
The duplicate_users will contain array of user id that have the same email like user_id
This is my main query but it’s really bad because i get to many results.
SELECT a.id user_id,
a.email,
b.id,
dup_user_id
FROM users a,
users b
WHERE a.email = b.email
AND a.id != b.id
Thanks in advanced.
What do you mean by “array of users?” Do you mean storing a collection data type? A CSV list? The term “Array” does not really fit within the Oracle sphere.
Also, by nature of what you are asking you want the key to be the email, not one of the IDs otherwise you get each combination.
For example users a and b each have email “bob@inter.net”. You query would have
a, bob@inter.net, b
b, bob@inter.net, a
And I think what you want is
bob@inter.net, (a,b)
Now, to make the field a CSV list of IDs you could use :
If you want to store the ids in an oracle collection, I could steer you in that direction too.
EDIT: based on your comment.
OK, if you want the full results, then ammend to
So if users a, b, and c all share email bob@inter.net you will get:
a, bob@inter.net, “b,c”
b, bob@inter.net, “a,c”
c, bob@inter.net, “a,b”
If you want to drop the email from the query, then:
Incidentally, if you are on an older version of Oracle that does not support the listagg string aggregation function, then you will be able to find an alternate solution here: http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php
I would suggest using the wm_concat() equivalent for Oracle 11.1 or 10, or one of the others for Oracle 9.
So, for Oracle 11.1 or 10, use: