I’m trying to work out if this is possible, let me give an example. Would be awesome if you could guide me in the right direction please.
Table = names
--------------------
Marks & Spencer
Marks & Spencer
marks & spencer
What I am trying to do is to return distinct values where I have converted all & signs and changed to upper case.
So my query is:
SELECT regexp_replace(UPPER(name), '&(amp;)*|\\+', '&', 'gi') AS name FROM names GROUP BY names;
However, what I would like to do is also return one of the original values, it does not matter which one, but I only want 1 row to be returned, like
Result
----------------
name original
------------------------
MARKS&SPENCER Marks & Spencer
Is this possible? Because at the moment, what I get returned is this:
Result
----------------
name original
------------------------
MARKS&SPENCER Marks & Spencer
MARKS&SPENCER Marks & Spencer
MARKS&SPENCER marks & spencer
Thank you for reading, would really appreciate the help.
==========
EDIT
The query I am using to get the above result is:
SELECT names.name, T.result FROM names
INNER JOIN
(
SELECT DISTINCT regexp_replace(UPPER(name), '&(amp;)*|\\+', '&', 'gi') AS result FROM names
) AS T
ON regexp_replace(UPPER(name), '&(amp;)*|\\+', '&', 'gi')=T.result
GROUP BY T.result, names.name
ORDER BY T.result ASC
I am using PostgreSQL btw, which can do more than MySQL incase that changes things?
You need to group by the new name to get only one row and, as you don’t care which original name appears, aggregate it with something like
min: