[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 84
  • Last Modified:

oracle sql syntax - concatenate unique values

I've got a series of names in up to eight columns (Name1 - Name8).

I'd like to combine the unique values into a new column - names.


is there a way of doing this without a loads of case statements eg:

when FamilySize= 1 then X1

when FamilySize= 2 then 
  (case when Name1 = Name2 then Name1 else Name1 || ' / ' || Name2  end)

when FamilySize= 3 then
  when Name1 = Name2 and Name2 = Name3 then Name1 
  when Name1 = Name2 and Name2 <> Name3 then Name1 || ' / ' || Name3 
  when Name1 <> Name2 and Name2 <> Name3 then Name1 || ' / ' || Name2 || ' / ' || Name3 end)

when FamilySize= 4 then
  when Name1 = Name2 and Name2 = Name3 and Name3 = Name4 then Name1 
  when Name1 = Name2 and Name2 = Name3 and Name3 <> Name4 then Name1 || ' / ' || Name2 

end names

Open in new window

  • 2
  • 2
1 Solution
Alexander Eßer [Alex140181]Software DeveloperCommented:
tonMachine100Author Commented:
this is not the same question. i do want the same output, but the dataset i was working with in the previous question


is not the same as the dataset im working with in this question.

Alexander Eßer [Alex140181]Software DeveloperCommented:
I don't see it that way, but anyways...
Btw: the column 'FamilySize' is 1. redundant because its value can be derived from the name columns (if filled or not) and 2. might by dangerous to use then (you may end up in a mess if this is your base for string formatting -> e.g. "if FamilySize = 3 .... " -> what happens if only 2 name columns were filled?!)
You should really avoid things like that and clean up your DB/table structure ;-)
slightwv (䄆 Netminder) Commented:
Pretty much the same.  All you need to do remove the dupes across the columns.

Here is what I came up with:
drop table tab1 purge;
create table tab1(familyid number, name1 varchar2(10), name2 varchar2(10), name3 varchar2(10));
insert into tab1 values(1,'a','a','a');
insert into tab1 values(2,'c','a','a');
insert into tab1 values(3,'c','b','a');

SELECT familyid,
            EXTRACT(XMLAGG(XMLELEMENT("s", column_value || '/')), '/s/text()').getstringval(),
        ) mynames
from tab1, table(set(ora_mining_varchar2_nt(name1,name2,name3))) e
group by familyid

Open in new window

slightwv (䄆 Netminder) Commented:
Sorry to object but #a41810859 produces the exact required output.

It also uses the set operator which I've not seen a lot of out there.  I've seen even less use of the ora_mining_varchar2_nt build in collection.

I would hate to lose that from the site.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now