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 AS COLUMNS

Author  Topic 

anlefi
Starting Member

4 Posts

Posted - 2009-02-28 : 13:49:10
Hi All,

I have the following tables in my database:

---------------------------------------
| Question |
---------------------------------------
| QuestionId * | QuestionText |
---------------------------------------
| 1 | What's your age? |
| 2 | Favorite color? |
---------------------------------------

------------------------------------------
| Response |
------------------------------------------
| UserId* | QuestiondId* | ResponseValue |
------------------------------------------
| 1 | 1 | "22" |
| 1 | 2 | "Grey" |
| 2 | 1 | "33" |
| 2 | 2 | NULL |
------------------------------------------

What I want to do is to obtain for each user all the responses and the QuestionText as the column title in only one query, something like:


** DESIRED RESULT **
-----------------------------------------------
| UserId | What's your age? | Favorite color? |
-----------------------------------------------
| 1 | "22" | "Grey" |
| 2 | "33" | NULL |
-----------------------------------------------

Does anyone have an idea if it is possible with only one query and give me a clue about how to do this?
Thanks in advance!!!

Anlefi

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2009-02-28 : 16:18:44
[code]
select

r.UserId as UserId
,q.QuestionText as [What's your age?]
,q.ResponseValue as [Favorite color?]
from
question as q
inner join response as r
on q.questionId = r.questiondId
where
UserId = 1
[/code]

best
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-03-02 : 01:00:59
[code]
try this by dyanmic cross tab
declare @tab table( UserId int,QuestionId int,ResponseValue varchar(32))
insert into @tab select
1, 1, '22' union all select
1, 2, 'Grey' union all select
2, 1, '33' union all select
2, 2, NULL

declare @tab1 table(QuestionId int,QuestionText varchar(32))
insert into @tab1 select
1, 'Whats your age?' union all select
2, 'Favorite color?'

select userid, max(case when questiontext ='Whats your age?' then responsevalue end) as 'Whats your age?',
max(case when questiontext ='Favorite color?' then responsevalue end) as 'Favorite color?'
from
(select userid , QuestionText,ResponseValue
from @tab1 t
inner join
@tab q on q.questionid = t.questionid ) t
group by userid
[/code]
Go to Top of Page

Mangal Pardeshi
Posting Yak Master

110 Posts

Posted - 2009-03-02 : 01:11:16
Look for PIVOT in BOL -
http://msdn.microsoft.com/en-us/library/ms177410(SQL.90).aspx


declare @response table( UserId int,QuestionId int,ResponseValue varchar(32))
insert into @response select
1, 1, '22' union all select
1, 2, 'Grey' union all select
2, 1, '33' union all select
2, 2, NULL

declare @question table(QuestionId int,QuestionText varchar(32))
insert into @question select
1, 'Whats your age?' union all select
2, 'Favorite color?'


SELECT UserId, [Whats your age?],[Favorite color?]
FROM ( SELECT R.userId, Q.QuestionText,R.ResponseValue
FROM @question Q INNER JOIN @response R
ON q.QuestionId = r.QuestionId
) Main
PIVOT
(
MAX(ResponseValue)
FOR QuestionText IN ([Whats your age?],[Favorite color?])

)pVT


Mangal Pardeshi
http://mangalpardeshi.blogspot.com
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-03-02 : 01:15:25
quote:
Originally posted by slimt_slimt


select

r.UserId as UserId
,q.QuestionText as [What's your age?]
,q.ResponseValue as [Favorite color?]
from
question as q
inner join response as r
on q.questionId = r.questiondId
where
UserId = 1

best


i will not give u the required output once check with sample data
Go to Top of Page
   

- Advertisement -