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
 "JOINING" Column Names

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-03-26 : 13:34:50
Hi All -I have the following two tables:
[CODE]
DATA_TABLE_1

RECORD_NO Q1 Q2 Q3 Q4 Q5 Q6
100234566  1  0  1  1  1  1  0
100234567  0  0  1  0  1  1  0
100234568  1  0  1  1  0  1  0
100234569  0  0  0  1  1  0  0

QUESTION_TABLE

QUESTION_ID QUESTION_DESC
Q1         Were you Greeted
Q2     Was the staff helpful
Q3     Would you visit again
Q4     Was the store clean
Q5     Are the hours convenient
Q6     Questions/Comments
[/CODE]
I need to output everything in Table one, but with the "Friendly" Question Names as the column names, instead of the Question codes.

Currently, I have it set up like this for the meantime:
[CODE]
SELECT [RECORD_NO]
,[Q1] AS 'Were you Greeted'
,[Q2] AS 'Was the staff helpful'
,[Q3] AS 'Would you visit again'
,[Q4] AS 'Was the store clean'
,[Q5] AS 'Are the hours convenient'
,[Q6] AS 'Questions/Comments'
FROM DATA_TABLE_1
[/CODE]
However there's over 50 questions, which change on a regular basis. Is there a way to 'join' the two tables so it can output the question_desc on a join, rather than having to manually assign aliases to the column names?

THANKS!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-26 : 13:37:55
you need to use pivot for that.
see

http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

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

Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-03-26 : 13:40:22
Thanks, but the data doesn't need to be pivot in any fashion.
The layout is the same, I just need the column names in the first name modified in an automated fashion.

Any ideas?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-26 : 14:44:35
it needs to be unpivoted and pivoted back if you need value as headers


SELECT *
FROM
(
SELECT RECORD_NO,Ans,QUESTION_DESC
FROM
(
SELECT RECORD_NO,QN,Ans
FROM DATA_TABLE_1 t
UNPIVOT (Ans FOR QN IN (Q1, Q2, Q3, Q4, Q5, Q6))u
)m
INNER JOIN QUESTION_TABLE n
ON n.QUESTION_ID = m.QN
)t
PIVOT (SUM(Ans) FOR QUESTION_DESC IN ([Were you Greeted],[Was the staff helpful],...))p



and if you want to make it dynamic refer modification in earlier link

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

Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-03-27 : 09:28:12
Okay, Thank you sir !!!!!!!!!!!!!!!!!!!!!!!!!!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-27 : 11:54:50
welcome

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

Go to Top of Page
   

- Advertisement -