Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Rows data To Column

Author  Topic 

GhantaBro
Posting Yak Master

215 Posts

Posted - 2011-12-05 : 13:02:41
Hi Guys,

I need to find a way where I can dynamically create columns for the data rows. I need to create a proc or function to do something like below:
Create Table dbo.EmpProj 
( EmpID int,
ProjectID int,
ProjRole varchar(1000),
HoursWorked int
)

Insert into EmpProj Values(1, 1, 'Create Etl', 85)
Insert into EmpProj Values(1, 2, 'Analyze Data', 20)
Insert into EmpProj Values(1, 3, 'Create Update Processes', 120)
Insert into EmpProj Values(2, 5, 'QA', 30)
Insert into EmpProj Values(2, 1, 'Test Scripts', 25)
Insert into EmpProj Values(2, 7, 'Test ETL', 16)
Insert into EmpProj Values(3, 2, 'Create Documents', 40)

Create Table dbo.Project
( ProjectID int,
ProjectName varchar(1000)
)

Insert into Project Values(1, 'ABC Project')
Insert into Project Values(2, 'AAA Project')
Insert into Project Values(3, 'XYZ Project')
Insert into Project Values(4, 'Test Project')
Insert into Project Values(5, 'OOO Project')
Insert into Project Values(6, 'PETProject')
Insert into Project Values(7, 'AlB Project')
Insert into Project Values(8, 'ZXW Project')


--This is the output I need using the data in the above two tables:

--I need to create a function or SP where I should be able to Pass EmpID and get the every project's information in columns
--And EmpID can be associated with serveral projects And I cannot hardcode project IDs as it will not be static and increase with time for newer projects!!!!!

--For Example for for EmpID = 1 we will have the output as below:

EmpID ProjectName_1 ProjRole_1 HoursWorked_1 ProjectName_2 ProjRole_2 HoursWorked_2 ProjectName_3 ProjRole_3 HoursWorked_3
--==================================================================================================================================================
1 ABC Project Create Etl 85 AAA Project Analyze Data 20 XYZ Project Create Update Processes 120

Please suggest me the best way to tackle this type of request if we can in SQL?

Thanks a lot for taking time in responding this...

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-05 : 13:04:18
see

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GhantaBro
Posting Yak Master

215 Posts

Posted - 2011-12-05 : 16:32:21
Viskah thanks for the link. It is good; however, I do not need to summarize or aggregate any data.. just need to transpose couple of rows into column and EmpID should be the unique column after transpose.

quote:
Originally posted by visakh16

see

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2011-12-05 : 16:48:07
Sample data

Create Table #EmpProj 
( EmpID int,
ProjectID int,
ProjRole varchar(1000),
HoursWorked int
)

Insert into #EmpProj Values(1, 1, 'Create Etl', 85)
Insert into #EmpProj Values(1, 2, 'Analyze Data', 20)
Insert into #EmpProj Values(1, 3, 'Create Update Processes', 120)
Insert into #EmpProj Values(2, 5, 'QA', 30)
Insert into #EmpProj Values(2, 1, 'Test Scripts', 25)
Insert into #EmpProj Values(2, 7, 'Test ETL', 16)
Insert into #EmpProj Values(3, 2, 'Create Documents', 40)

Create Table #Project
( ProjectID int,
ProjectName varchar(1000)
)

Insert into #Project Values(1, 'ABC Project')
Insert into #Project Values(2, 'AAA Project')
Insert into #Project Values(3, 'XYZ Project')
Insert into #Project Values(4, 'Test Project')
Insert into #Project Values(5, 'OOO Project')
Insert into #Project Values(6, 'PETProject')
Insert into #Project Values(7, 'AlB Project')
Insert into #Project Values(8, 'ZXW Project')


Query

select t.EmpID
,max(case when seq = 1 then ProjectName else null end) as [ProjectName_1]
,max(case when seq = 1 then ProjRole else null end) as [ProjectRole_1]
,max(case when seq = 1 then HoursWorked else null end) as [HoursWorked_1]
,max(case when seq = 2 then ProjectName else null end) as [ProjectName_2]
,max(case when seq = 2 then ProjRole else null end) as [ProjectRole_2]
,max(case when seq = 2 then HoursWorked else null end) as [HoursWorked_2]
,max(case when seq = 3 then ProjectName else null end) as [ProjectName_3]
,max(case when seq = 3 then ProjRole else null end) as [ProjectRole_3]
,max(case when seq = 3 then HoursWorked else null end) as [HoursWorked_3]
from
(
select row_number() over(partition by a.EmpID order by b.ProjectName) as seq, a.EmpID, b.ProjectName,a.ProjRole,a.HoursWorked from #EmpProj a
inner join #Project b on a.ProjectID = b.ProjectID
) t
group by t.EmpID


Result
EmpID       ProjectName_1        ProjectRole_1             HoursWorked_1 ProjectName_2        ProjectRole_2             HoursWorked_2 ProjectName_3        ProjectRole_3             HoursWorked_3
----------- -------------------- ------------------------- ------------- -------------------- ------------------------- ------------- -------------------- ------------------------- -------------
1 AAA Project Analyze Data 20 ABC Project Create Etl 85 XYZ Project Create Update Processes 120
2 ABC Project Test Scripts 25 AlB Project Test ETL 16 OOO Project QA 30
3 AAA Project Create Documents 40 NULL NULL NULL NULL NULL NULL
Go to Top of Page

GhantaBro
Posting Yak Master

215 Posts

Posted - 2011-12-05 : 19:11:19
Vijay thanks...this works well with the data we have, but the ProjectID will be dynamic... it will grow for some. I need to create the columns dynamically...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-06 : 00:04:48
quote:
Originally posted by GhantaBro

Vijay thanks...this works well with the data we have, but the ProjectID will be dynamic... it will grow for some. I need to create the columns dynamically...


thats what i've provided you with. the link shows how to do cross tabbing dynamic based on column values. You need to apply somekind of aggregation function even if you're not looking for summary here as cross tabbing itself is an aggregation operation.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GhantaBro
Posting Yak Master

215 Posts

Posted - 2011-12-06 : 09:52:52
Oh thanks a lot guys both Visakh and Vijay.
quote:
Originally posted by visakh16

quote:
Originally posted by GhantaBro

Vijay thanks...this works well with the data we have, but the ProjectID will be dynamic... it will grow for some. I need to create the columns dynamically...


thats what i've provided you with. the link shows how to do cross tabbing dynamic based on column values. You need to apply somekind of aggregation function even if you're not looking for summary here as cross tabbing itself is an aggregation operation.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-06 : 10:19:45
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -