1

I have a set of data that looks like this:

Before

FirstName   LastName   Field1   Field2   Field3 ... Field27
---------   --------   ------   ------   ------     -------
 Mark        Smith     A        B        C          D
 John        Baptist   X        T        Y          G
 Tom         Dumm      R        B        B          U

However, I'd like the data to look like this:

After

FirstName   LastName   Field   Value
---------   --------   -----   -----
Mark        Smith      1       A
Mark        Smith      2       B
Mark        Smith      3       C
Mark        Smith      4       D
John        Baptist    1       X
John        Baptist    2       T
John        Baptist    3       Y
John        Baptist    4       G
Tom         Dumm       1       R
Tom         Dumm       2       B
Tom         Dumm       3       B
Tom         Dumm       4       U

I have looked at the PIVOT function. It may work. I am not too sure. I couldn't make sense of how to use it. But, I am not sure that the pivot could place a '4' in the 'Field' column. From my understanding, the PIVOT function would simply transpose the values of Field1...Field27 into the 'Value' column.

I have also considered iterating over the table with a Cursor and then looping over the field columns, and then INSERTing into another table the 'Field's and 'Value's. However, I know this will impact performance since it's a serial-based operation.

Any help would be greatly appreciated! As you can tell, I'm quite new to T-SQL (or SQL in general) and SQL Server.

Community
  • 1
  • 1
supercoder
  • 107
  • 1
  • 2
  • 7

3 Answers3

4

You can perform with an UNPIVOT. There are two ways to do this:

1) In a Static Unpivot you would hard-code your Field columns in your query.

select firstname
  , lastname
  , replace(field, 'field', '')  as field
  , value
from test
unpivot
( 
  value
  for field in (field1, field2, field3, field27)
) u

See a SQL Fiddle for a working demo.

2) Or you could use a Dynamic Unpivot which will get the list of items to PIVOT when you run the SQL. The Dynamic is great if you have a large amount of fields that you will be unpivoting.

create table mytest
(
  firstname varchar(5),
  lastname varchar(10),
  field1 varchar(1),
  field2 varchar(1),
  field3 varchar(1),
  field27 varchar(1)
)

insert into mytest values('Mark', 'Smith', 'A', 'B', 'C', 'D')
insert into mytest values('John', 'Baptist', 'X', 'T', 'Y', 'G')
insert into mytest values('Tom', 'Dumm', 'R', 'B', 'B', 'U')

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

select @cols = stuff((select ','+quotename(C.name)
         from sys.columns as C
         where C.object_id = object_id('mytest') and
               C.name like 'Field%'
         for xml path('')), 1, 1, '')

set @query = 'SELECT firstname, lastname, replace(field, ''field'', '''')  as field, value
            from mytest
            unpivot 
            (
               value
               for field in (' + @cols + ')
            ) p '

execute(@query)

drop table mytest

Both will produce the same results.

Taryn
  • 242,637
  • 56
  • 362
  • 405
3

If you want to do it query than quick and dirty way will be to create Union

             Select FirstName,LastName,1,Field1
             from table
             UNION ALL
             Select FirstName,LastName,2,Field2
             from table
             . 
             .

And similar for all field cols

ejb_guy
  • 1,125
  • 6
  • 6
1

Rather than using pivot, use unpivot like this:

select firstname, lastname, substring(field,6,2) as field, value
from <yourtablename>
unpivot(value for field in (field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11,field12,field13,field14,field15,field16,field17,field18,field19,field20,field21,field22,field23,field24,field25,field26,field27,field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11,field12,field13,field14,field15,field16,field17,field18,field19,field20,field21,field22,field23,field24,field25,field26,field27)) as unpvt;
jon Z
  • 15,838
  • 1
  • 33
  • 35