Connecting Tech Pros Worldwide Forums | Help | Site Map

Convert Rows into Columns

Newbie
 
Join Date: Sep 2006
Posts: 1
#1: Sep 5 '06
Hi SMART GUYS,

Please help me write a query. Actually I want to convert my rows into columns. Can anyone kindly give me the query to do it? My rows are about employees. There can be any number of employees (in the following table I have 3 employees ABC, EFG, WRI) with any number of records for each employee (there can be 20, 30 records each), but all employees have the same number of records (in the following table each employee has 3 records). My table is:


USERID Name Role Total
100 ABC 1 0
100 ABC 1 0
100 ABC 1 1
101 EFG 1 0
101 EFG 1 2
101 EFG 1 1
102 WRI 2 1
102 WRI 2 0
102 WRI 2 1

I want to convert the above table in the following form:

USERID Name Role Total1 Total2 Total3
100 ABC 1 0 0 1
101 EFG 1 0 2 1
102 WRI 2 1 0 1

Columns [USERID], [Name], [Role] are unique for each row but Column [Total] is dynamic (in above case, 3 [Total] Columns Total1, Total2, Total3) and depends on the number of records against each employee. If there were 30 records for each employee, the Column [Total] will be (Total1, Total2, Total3…Total30), I have mentioned above that all employees will have the same number of records.

I have even used the Pivot Operator in SQL Server 2005 but it always end up in an error about the syntax proceeding the PIVOT word. I just need a query with/without the PIVOT operator.

I look forward to your reply. I will be highly obliged.

Regards...

Newbie
 
Join Date: Oct 2006
Posts: 1
#2: Oct 30 '06

re: Convert Rows into Columns


i think this will help you..

table design for me is like this

table name is::CandidateTemp

id FieldName Value

1 email aaa
1 skill c
1 name AAAAA
2 email aaa
2 skill c,java
2 name AAAAA
5 email aaa
5 skill c
5 name GGGG
9 email iii
9 skill c,vb,java
9 name AAAA



and i want this format

id email skill name

1 aaa c AAAAA
2 aaa c,java AAAAA
5 aaa c GGGG
9 iii c,vb,java AAAA


Query for this is



select a.id as id, a.Value as email, b.Value as skill, c.Value as name from CandidateTemp a,CandidateTemp b,CandidateTemp c where a.id=b.id and b.id=c.id
and a.FieldName='email' and b.FieldName='skill' and c.FieldName='name'
scripto's Avatar
Familiar Sight
 
Join Date: Oct 2006
Posts: 143
#3: Oct 31 '06

re: Convert Rows into Columns


Quote:

Originally Posted by dotnetguru

Hi SMART GUYS,

Regards...

This was a nice challenge - i created a new_employee table to hold the results.
here it is based on your specific requirements:

CREATE TABLE [dbo].[new_employees] (
[userid] [int] NOT NULL ,
[name] [varchar] (50) NOT NULL ,
[role] [int] NOT NULL
) ON [PRIMARY]
GO

-- backup your employees table first
alter table employees add [ID] int identity(1,1) not null
go

declare @max int, @min int
set @max = (select max(id) from employees)
set @min = (select min(id) from employees)

declare @totcols int, @counter int
set @totcols = (select count(*) from employees where userid = 100)
set @counter = 1

declare @str varchar(50)
declare @sqlStr nvarchar(255)


while @counter <= @totcols
begin
set @str = 'total'+ convert(varchar(5),@counter)

set @sqlStr = N'alter table new_employees add '+ @str + ' int null'
exec sp_executesql @sqlStr


set @counter = @counter + 1
set @str = ''
set @sqlStr = ''
end

insert into new_employees (USERID, Name, Role)
select distinct USERID, Name, Role from employees


while @min <= @max
begin
set @counter = 1
while @counter <= @totcols
begin
set @str = 'total'+ convert(varchar(5),@counter)

set @sqlStr = N'update new_employees set ' + @str + ' = (select total from employees where id = ' + convert(varchar(5),@min) + ')' +
' where userid = (select userid from employees where id = ' + convert(varchar(5),@min) + ')'
exec sp_executesql @sqlStr

set @counter = @counter + 1
set @min = @min + 1
set @str = ''
set @sqlStr = ''

end


end



select * from new_employees
Newbie
 
Join Date: Dec 2006
Posts: 1
#4: Aug 9 '07

re: Convert Rows into Columns


I have a table "parameterData" look like this:

ParID TimeStamp Scale Value
1 8/9/2007 10:48:46 AM 10 1500
2 8/9/2007 10:48:46 AM 10 1610
3 8/9/2007 10:48:46 AM 10 1640
4 8/9/2007 10:48:46 AM 10 1580
5 8/9/2007 10:48:46 AM 0.1 219.2
6 8/9/2007 10:48:46 AM 0.1 332.6
7 8/9/2007 10:48:46 AM 0.1 219.3
8 8/9/2007 10:48:46 AM 0.1 257
1 8/9/2007 10:50:06 AM 10 1570
2 8/9/2007 10:50:06 AM 10 1700
3 8/9/2007 10:50:06 AM 10 1470
4 8/9/2007 10:50:06 AM 10 1570
5 8/9/2007 10:50:06 AM 0.1 219.6
6 8/9/2007 10:50:06 AM 0.1 332.6
7 8/9/2007 10:50:06 AM 0.1 219.2
8 8/9/2007 10:50:06 AM 0.1 257

Timestamp value for all-eight row (parID 1-8) is same
I want to have a table where column like this
TimeStamp 1 2 3 4 5 6 7 8


How do i do this?

thanx
Newbie
 
Join Date: Feb 2008
Posts: 1
#5: Feb 17 '08

re: Convert Rows into Columns


Here is a Query:-
SELECT USERID,NAME,ROLE,MAX(DECODE(R,1,TOTAL)) TOT1,
MAX(DECODE(R,2,TOTAL)) TOT2,
MAX(DECODE(R,3,TOTAL)) TOT3
FROM (SELECT USERID,NAME,ROLE,TOTAL
,ROW_NUMBER() OVER(PARTITION BY USERID ORDER BY USERID) R
FROM T3)
GROUP BY USERID,NAME,ROLE

----------output----------------------------------------------------------------------------

USERID NAME ROLE TOT1 TOT2 TOT3
---------- ----- ---------- ---------- ---------- ----------
100 ABC 1 0 0 1
101 EFG 1 0 2 1
102 WRI 2 1 0 1
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,992
#6: Feb 17 '08

re: Convert Rows into Columns


If you're using SQL 2005, you're in luck. Try here

-- CK
Newbie
 
Join Date: Jul 2008
Posts: 2
#7: May 29 '09

re: Convert Rows into Columns


PIVOT relational operator in sql server 2005 can be used to convert rows to columns

http://www.logiclabz.com/sql-server/...ing-pivot.aspx
Reply