Solved

oracle sql syntax - concatenate unique values

Posted on 2016-09-21
7
29 Views
Last Modified: 2016-11-02
I've got a series of names in up to eight columns (Name1 - Name8).

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

ee4

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

case 
when FamilySize= 1 then X1

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

when FamilySize= 3 then
  (case 
  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
  (case 
  when Name1 = Name2 and Name2 = Name3 and Name3 = Name4 then Name1 
  when Name1 = Name2 and Name2 = Name3 and Name3 <> Name4 then Name1 || ' / ' || Name2 

etc....
end names

Open in new window



thanks
0
Comment
Question by:tonMachine100
  • 2
  • 2
7 Comments
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 41808945
0
 

Author Comment

by:tonMachine100
ID: 41810273
this is not the same question. i do want the same output, but the dataset i was working with in the previous question

fig10.PNG

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

fig11.PNG
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 41810285
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 ;-)
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 41810859
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');
commit;
*/

SELECT familyid,
        RTRIM(
            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

0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41869266
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.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
oracle query help 18 78
how to remove non-duplicated row 5 25
query returning everything 11 70
VB.Net - CSV to Oracle table 4 32
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now