I have the below table:
create table test(
A1 varchar,
A2 varchar,
A3 varchar,
B1 varchar,
B2 varchar,
B3 varchar
);
insert into test values('How', 'Are', 'You', 'You', 'How', 'Are');
insert into test values('How', null , 'You', 'You', 'How', null);
I want to concatenate all A strings and all B strings, sort them and compare the 2.
This gives me the concatenation:
select (coalesce(A1, '') || ' ' || coalesce(A2,'') || ' ' || coalesce(A3, '')) as A,
(coalesce(B1, '') || ' ' || coalesce(B2,'') || ' ' || coalesce(B3, '')) as B
from test;
This is the output:
A B
How Are You You How Are
How You You How
As seen, both of these strings are same if we sort them and compare. How can I do this in Postgres?
Here is the fiddle to try: