Sample Header Ad - 728x90

Match .csv values as INTs. If one value from a group in the .csv matches one in another group, then merge strings

5 votes
1 answer
224 views
Here we have two sets of numbers. The problem is that I can't figure out how to get from the input to the output of numbers (DDL and DML below and also in the fiddle here ). current_data 1,2,3 1,4 1,5,7 8,9,10 10,11,15 expected_outcome 1,2,3,4,5,7 8,9,10,11,15 We are simply trying to match a group of numbers based on if a single number matches in any other group. Then merge all of these groups. For instance. If we have: ('1,2,3'), ('1,4'), ('1,5,7') We want: (1,2,3,4,5,7) We consolidate them into a single line in PostgreSQL. or (another example): ('8,9,10'), ('10,11,15') desired output: (8,9,10,11,15) The query would group these numbers because they have the number 10 in common. But it wouldn't group with the previous row (i.e. (1,2,3,4,5,7)) that don't share a number. When we have these groups in a table. They will only group together if they at least have one matching number in each group. ======== DDL and DML ============= create table current (current_data text not null); create table expected_output (expected_outcome text not null); insert into current (current_data) values ('1,2,3'),('1,4'),('1,5,7'),('8,9,10'), ('10,11,15'); insert into expected_output (expected_outcome) values ('1,2,3,4,5,7'),('8,9,10,11,15');
Asked by user3050153 (67 rep)
Aug 16, 2021, 05:08 PM
Last activity: Aug 29, 2021, 04:46 PM