We help IT Professionals succeed at work.

Rows to Columns

446 Views
Last Modified: 2012-05-09
Hi.
I have this sample tables
Table A
Field1  | FK Table2
Name1      | 10
Name2      | 20
Name2      | 30
Name3      | 10
Name2      | 20

Table B
Field1  | Field 2
10      | TTT
10      | XXX
20      | TTT
10      | ZZZ
30      | TTT

And i want to obtain a table with table b rows as columns
something like this

Table

Name1      | TTT | XXX | ZZZ
Name2      | TTT |     |
Name2      | TTT |     |
Name3      | TTT | XXX | ZZZ
Name2      | AAA |     |
Comment
Watch Question

Dear ,

if " FK Table2" of tableA is a refrence key of TableB than you can simply use the below query

Select a.*,b.* from TableA as a join TableB as b on a.[FK Table2]=b.Field1

it will surly get the desired result. if the above condition is tru.

thanks
Dear,

Sorry ignore my privious post, wrongly intercepeted your requirement, will update you shortly with the right query for the required result set.

thanks

Commented:
A simple solution will be to create a single comma delimited column like this
select  distinct
 a.Field1,
 stuff((select ',' + Field2 from TableB where Field2 = a.FKTable2 for xml path('')), 1, 1, '') as Fields2
from TableA a
If you still prefer to have separate columns for each FKTable2 then can do something like this:
 

Declare @strSQL varchar(max)        
Declare @cols varchar(2000)        
      
SELECT  @cols = STUFF((SELECT DISTINCT         
                                '],[' + cast(Field1 as varchar)
                        From TableB   
                        ORDER BY 1        
                        FOR XML PATH('')        
                      ), 1, 2, '') + ']'        
         
         
set @strSQL = 'select a.Field1, ' + @cols +       
' from (       
        Select a.Field1, a.FKTable2, b.Field2, from TableA a
	inner join TableB b on a.FKTable2 = b.Field1
) o       
pivot (max(Field2) for FkTable2 in (' + @cols + ')) as p'     
     
exec(@strSQL)

Open in new window

Author

Commented:
thanks ramalda, you answer do the job bu i cant put it work with columns
My first table name was a bit confusing

Table A
Key1  | FK1
Name1      | 10
Name2      | 20
Name2      | 30
Name3      | 10
Name2      | 20

Table B
Key2  | Field2
10      | TTT
10      | XXX
20      | TTT
10      | ZZZ
30      | TTT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.