tags:

views:

107

answers:

4

I have a table such as:

John email1 email2 email3 ...and so on

I need to transform the dataset to this format:

John email1
John email2
John email3

it could be exported out to Excel and the imported back in if that would be easier. Thanks...

+3  A: 

What do you mean fix? You could just do something like this:

SELECT name+' '+email1 as email1,
       name+' '+email2 as email2, 
       name+' '+email3 as email3, 
       name+' '+email3 as email3, 
       ...
FROM table

The above was written before I saw the formating ... this I believe is what you want -- only tested on mssqlserver

declare @t table
(
  name varchar(max),
  email1 varchar(max),
  email2 varchar(max),
  email3 varchar(max),
  email4 varchar(max)
)

insert into @t 
 values ('name1a','email1a','email2a','email3a','email4a')
insert into @t  
 values ('name2b','email1b','email2b','email3b','email4b')

 select * from @t

SELECT name, email 
FROM
(
SELECT name, email1, email2, email3, email4
FROM @t) p
UNPIVOT
   (email FOR emails IN 
      (email1, email2, email3, email4)
)AS unpvt
  
Hogan
oh it changed...
Hogan
UNPIVOT is exactly what is needed. A post below has a union as an answer, which works just as well, it's just the UNPIVOT is easier (for me) to read and understand.
Richard Brust
@Richard - if this is the answer you like then click the check box (and up-vote) and the question will close. Welcome to StackOverflow.
Hogan
A: 

I see no reasons to "go out" to XLS or something else and then re-importing.

You can fix the situation by

1) (only if deemed worthy, depending on DB size, number of rows etc.), possibly drop a few indexes on the table.

2) Running INSERT queries to add the new rows, i.e. these made from the Name and the "Emailn" columns (along with other desired columns or desired default values)

3) Once all the "Emailn" columns have been dispatched, Altering the table schema to drop these columns.

4) Re-building the indexes dropped earlier and/or re-packing the others.

Specifically, for #2, the query(ies) would look like

INSERT INTO MyTable 
   (Name, Email1, SomeOtherColumn, YetOtherColumn)
   SELECT Name, Email2, someColumn, "ABC"
   FROM MyTable
   WHERE Email2 IS NOT NULL

You do this for each "Emailn" column beyond Email1 of course. Et voila...

Only possibly difficulties when the table is very big etc, but since you were considering Excel, that's probably not the case.

mjv
thanks mjv, you are correct in that the export/import using Excel is just two extra steps that don't need to be taken. I'm acutally taking this data and adding more rows to the table it's currently in. There are over 11000 names, probably 8000 have multiple emails. After this is done, your step #3 is exactly what the plan is. Thanks...
Richard Brust
+3  A: 

Assuming that Name, Email1, Email2, Email3 are the column names:

   Select Name, Email1
    From YourTable
    Where Email1 <> ''
    Union
    Select Name, Email2
    From YourTable
    Where Email2 <> ''
    Union
    Select Name, Email3
    From YourTable
    Where Email3 <> ''
Lynette Duffy
Make sure to sort by Name when you're done to get all the email addresses for the same name together.
David Oneill
Note: with this, you'll have to have the number of unions of the longest line IE if you have 7 email addresses for one person, you'll need to have 7 unions. Scale as needed.
David Oneill
This works too. No fancy unpivot needed, nice! +1
Hogan
Note: this answer does answers the edited question, not the original question...
David Oneill
Thanks Lynette, this is great info. I think I like the unpivot, but that's just a personal preference.
Richard Brust
A: 

Complete overhaul (My original answer was based on a short-lived incorrect version of the question).

If "flat file" refers to a text file, then a simple (non-SQL) version for the conversion would be to use awk with something such as this:

awk "{ for (i = 2; i <= NF; i++ ) { print $1, $i }}" < original.txt
Mark Wilkins
Does this actually accomplish what he wants: IE get each email address in its own row?
David Oneill
I'm not sure. The original post by Richard showed it entirely in one row. It was a different person who changed it to one per row, which of course makes more sense but was not the original question.
Mark Wilkins
Yeah, i just noticed that... I wonder what the OP meant?
David Oneill
@Mark I think it was the original question just badly formated by Lynette
Hogan
I guess we'll eventually find out when he returns and checks. He has plenty of answers to choose from now. Kind of funny. We are all madly giving answers to what might not be a question.
Mark Wilkins
Thanks for the response, but it is a table, and it was all on one row - Name, email1, email2, email3, and so on. My apologies if I was unclear...
Richard Brust
No problem. I had fun thinking about it either way.
Mark Wilkins