I created the following view "user_details_merged":
SELECT DISTINCT coalesce(own.user_name, join_user_name.user_name) AS user_name, coalesce(own.email, join_mail.email) AS email, coalesce(own.first_name, join_name.first_name) AS first_name, coalesce(own.last_name, join_name.last_name) AS last_name FROM user_details AS own LEFT JOIN user_details AS join_user_name ON own.user_name IS NULL AND ( (join_user_name.email = own.email AND own.email IS NOT NULL) OR (join_user_name.first_name = own.first_name AND join_user_name.last_name = own.last_name AND own.first_name IS NOT NULL AND own.last_name IS NOT NULL)) LEFT JOIN user_details AS join_mail ON own.email IS NULL AND ( (join_mail.user_name = own.user_name AND own.user_name IS NOT NULL) OR (join_mail.first_name = own.first_name AND join_mail.last_name = own.last_name AND own.first_name IS NOT NULL AND own.last_name IS NOT NULL)) LEFT JOIN user_details AS join_name ON own.first_name IS NULL AND own.last_name IS NULL AND ( (join_name.email = own.email AND own.email IS NOT NULL) OR (join_name.user_name = own.user_name AND own.user_name IS NOT NULL)) ORDER BY user_name ASC,email ASC, first_name ASC, last_name ASC
This combines my columns with:
user_name | email | first_name | last_name ab NULL NULL NULL bcd a NULL ef NULL xyz
to
user_name | email | first_name | last_name ab NULL NULL NULL bcd a NULL ef NULL xyz abcd abef
I want to:
user_name | email | first_name | last_name NULL xyz abcd abef
without NULL containing ROWS when there is a line with the same data that has more information but still retain / NULL xyz / when there is no other line with additional information.
This second view here does exactly what I need:
SELECT DISTINCT a.user_name,a.email,a.first_name,a.last_name FROM user_details_merged a LEFT JOIN user_details_merged b ON ( ( a.user_name IS NOT NULL OR NOT EXISTS (SELECT user_name FROM user_details_merged b WHERE b.user_name IS NOT NULL AND b.email=ISNULL(a.email,b.email) AND b.first_name=isnull(a.first_name,b.first_name) AND b.last_name=isnull(a.last_name,b.last_name)) ) AND ( a.email IS NOT NULL OR NOT EXISTS (SELECT email FROM user_details_merged b WHERE b.email IS NOT NULL AND b.user_name=ISNULL(a.user_name,b.user_name) AND b.first_name=isnull(a.first_name,b.first_name) AND b.last_name=isnull(a.last_name,b.last_name)) ) AND ( (a.first_name IS NOT NULL AND a.last_name IS NOT NULL) OR NOT EXISTS (SELECT email FROM user_details_merged b WHERE b.email IS NOT NULL AND b.user_name=ISNULL(a.user_name,b.user_name) AND b.email=ISNULL(a.email,b.email))
The main problem is that the user_details view where the data comes from consists of many joins of different tables. Some of them contain only the username and email address, as well as only the email address and first / last name, etc. Therefore, there is no unique key, and I cannot index the view due to UNIONS. This makes it impossible to complete the last presentation in an hour. My current workaround is a procedure that stores data in the user_details_merged view in the temp table and allows the second view to use the data from this table. This way I can reduce the runtime to 7 seconds for 8000 lines.
Any other suggestions?
Thank you very much;)