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.
Author |
Topic |
gsolares
Starting Member
7 Posts |
Posted - 2008-10-06 : 15:13:13
|
I might be doing the wrong thing altogether, there must be a simple way to do what I want to, so please feel free to point that out.Simplified I have a table with a questionnaire:Fields: number, questionExample:number question1 what2 where3 whenAnd another table with users responsesFields: userID, what, where, whenExampleuserID what where when001 party home fridayWhat I would like to get from a query is:For userID=001number question answer1 what party2 where home3 when fridayAny ideas will be greatly appreciated.Cheers,GSThanks,GS |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-06 : 15:17:06
|
http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx E 12°55'05.63"N 56°04'39.26" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
gsolares
Starting Member
7 Posts |
Posted - 2008-10-06 : 16:10:49
|
Sorry, here is the full info...CREATE TABLE [dbo].[Questionnaire] ( [Number] [int] IDENTITY (1, 1) NOT NULL , [Question] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [QuestionID] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL) ON [PRIMARY]GOINSERT INTO Questionnaire(Question, QuestionID)SELECT 'What', 'ID01' UNION ALLSELECT 'Where', 'ID02' UNION ALLSELECT 'When', 'ID03'CREATE TABLE [dbo].[Q_Data] ( [UserID] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [ID01] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [ID02] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [ID03] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL) ON [PRIMARY]GOINSERT INTO Q_Data(UserID, ID01, ID02, ID03)SELECT 'User01', 'Party', 'Here', 'Now' UNION ALLSELECT 'User02', 'Work', 'There', 'Tomorrow'I need to create a query to get the following result for User01:Number Question Answer1 What Party2 Where Here3 When NowHope this is a little better.Thanks,GS |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-06 : 16:19:37
|
[code]DECLARE @Questionnaire TABLE([Number] [int] IDENTITY (1, 1) NOT NULL ,[Question] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[QuestionID] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL)INSERT INTO @Questionnaire(Question, QuestionID)SELECT 'What', 'ID01' UNION ALLSELECT 'Where', 'ID02' UNION ALLSELECT 'When', 'ID03'DECLARE @Q_Data TABLE ([UserID] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[ID01] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[ID02] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,[ID03] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL)INSERT INTO @Q_Data(UserID, ID01, ID02, ID03)SELECT 'User01', 'Party', 'Here', 'Now' UNION ALLSELECT 'User02', 'Work', 'There', 'Tomorrow'SELECT q.number, q.question, x.answerFROM @Questionnaire AS qLEFT JOIN ( SELECT 'ID01' AS col, ID01 AS answer FROM @Q_Data WHERE userid = 'user01' union all SELECT 'ID02' AS col, ID02 FROM @Q_Data WHERE userid = 'user01' union all SELECT 'ID03' AS col, ID03 FROM @Q_Data WHERE userid = 'user01' ) AS x ON x.col = q.QuestionID[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
gsolares
Starting Member
7 Posts |
Posted - 2008-10-06 : 16:27:28
|
Brilliant, thanks a lot!Thanks,GS |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-06 : 16:29:13
|
9 minutes is not that bad, right?What will you do next time you have a question?  E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|
|