SQLTeam.com | Weblogs | Forums

Pivot results


#1

i would like to pivot the following resuts in my table . employee


#2

Is the top row the "from" and the bottom row the "to" or vice versa?

Please post data in usable form, i.e., CREATE TABLE and INSERT, not just a picture, which we can't use to test any code we write for you.


#3


#4

a simple select statement

select name1,name2,name3 ,phone1, phone2 ,phone3 ,email1, email2, email3
from employee


#5

Again, no data, no way to test, but this should at least be very close:

select name AS Managers, phone, email
from dbo.employee
cross apply ( values(name1,phone1,email1),(name2,phone2,email2),(name3,phone3,email3) ) 
    as data(name, phone, email)

#6

you need to provide data as follows

if exists(select 1 from sys.tables where name = 'Dstyles')
	begin
		drop table Dstyles
	end;

create table dbo.Dstyles
(
ID INT IDENTITY(1,1) ,
name varchar(50),
phone varchar (50),
email varchar (50),)

insert dbo.Dstyles values ('john doe','407-512-1425', 'jdoews@Dstyles.com')
insert dbo.Dstyles values ('joe smith doe','407-512-1426', 'jsmith@Dstyles.com')
insert dbo.Dstyles values ('ken white','407-512-1475', 'kwhite@Dstyles.com')

#7

I've reworked the data yosiasz provided. I don't normally do this because I simply don't have time to do so. We're already volunteering our own time to write poster's queries, I simply don't have time to prep test data for everyone as well.

create table dbo.employee
(
name1 varchar(80),
name2 varchar(80),
name3 varchar(80),
phone1 varchar(30),
phone2 varchar(30),
phone3 varchar(30),
email1 varchar(80),
email2 varchar(80),
email3 varchar(80)
)

insert dbo.employee ( name1, phone1, email1, name2, phone2, email2, name3, phone3, email3 )  values 
    ('john doe','407-512-1425', 'jdoews@Dstyles.com',
     'joe smith doe','407-512-1426', 'jsmith@Dstyles.com',
     'ken white','407-512-1475', 'kwhite@Dstyles.com')

select name AS Managers, phone, email
from dbo.employee
cross apply ( values(name1,phone1,email1),(name2,phone2,email2),(name3,phone3,email3) ) 
    as data(name, phone, email)

#8

thx i reworked the above and got what i needed