SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Consolidating Rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

shutch00
Starting Member

3 Posts

Posted - 04/10/2013 :  12:35:14  Show Profile  Reply with Quote
I have a query that results in the following:

User Fac Role
AAA 71 TestA
AAA 71 TestB
AAA 71 TestC
BBB 208 TestB
BBB 208 TestC

User AAA has 3 different roles; TestA, TestB, and TestC

I'd like to see this arranged so that those 3 roles show in a single row.

User Fac Role1 Role2 Role3
AAA 71 TestA TestB TestC
BBB 208 TestB TestC

Is this possible?

Edited by - shutch00 on 04/10/2013 12:37:05

James K
Flowing Fount of Yak Knowledge

3562 Posts

Posted - 04/10/2013 :  12:40:57  Show Profile  Reply with Quote
Yes, you can use the PIVOT operator - like shown below:

select User, Fac, [1] as Role1, [2] as Role2, [3] as Role3 from
(
	select *,
		ROW_NUMBER() over (partition by User,fac order by Role) as N
	from
		Tbl
)s
pivot( max(role) for N in ([1],[2],[3]))P
Go to Top of Page

shutch00
Starting Member

3 Posts

Posted - 04/10/2013 :  13:02:21  Show Profile  Reply with Quote
Thanks, James K!

One additional wrench is that the number of roles varies. I listed TestA, TestB, and TestC, but in truth there could be any number of roles as this list gets added to often.

So tomorrow we may have additional roles of TestD and TestE.

I guess what I'm asking is: Can I create a dynamic pivot?

Edited by - shutch00 on 04/10/2013 13:02:38
Go to Top of Page

shutch00
Starting Member

3 Posts

Posted - 04/10/2013 :  13:28:13  Show Profile  Reply with Quote
I was able to use your query and pull the data I needed. I also added exra role assignments, so if any new ones pop up it will just populate the extra assigments. Thanks again!
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3562 Posts

Posted - 04/10/2013 :  13:50:26  Show Profile  Reply with Quote
Unfortunately, T-SQL natively does not let you use dynamic pivot columns. So the only way to make it dynamic is to use dynamic queries. See this blog for an example of how to use dynamic pivoting: http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4612 Posts

Posted - 04/10/2013 :  13:51:57  Show Profile  Reply with Quote
quote:
Originally posted by shutch00

I guess what I'm asking is: Can I create a dynamic pivot?

Sure, here is one way:

http://www.mssqltips.com/sqlservertip/2783/script-to-create-dynamic-pivot-queries-in-sql-server/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000