December 13, 2006 at 9:06 am
This is driving me nuts, all pivot/crosstab procedures I have found are related to aggregate functions.
My sql query is:
SELECT TOP 100 PERCENT dbo.ENC_Questions.Question, dbo.ENC_ANS.Answer, dbo.ENC_ANS.SessionID
FROM dbo.ENC_Questions INNER JOIN
dbo.ENC_ANS ON dbo.ENC_ANS.IDQuestion = dbo.ENC_AND.IDQuestion
WHERE (dbo.ENC_Questions.IDSurvey = 2155)
ORDER BY dbo.ENC_Questions.Order
December 13, 2006 at 9:12 am
Felipe
Can we see the DDL for the tables, please?
John
December 13, 2006 at 9:14 am
John,
Sory but what do you mean by DDL?
FBP
December 13, 2006 at 9:21 am
Felipe
Data Definition Language. In other words, the CREATE TABLE statements.
Thanks
John
December 13, 2006 at 9:26 am
John
Two related tables, Questions and Answers:
Table Questions:
[IDQuestion] [int] IDENTITY (4125, 1) NOT NULL ,
[IDSurvey] [int] NOT NULL ,
[Question] [nvarchar] (800) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[NPage] [int] NOT NULL ,
[Order] [int] NOT NULL CONSTRAINT [DF_ENC_Preguntas_Orden] DEFAULT (1),
[ReqA] [int] NOT NULL CONSTRAINT [DF_ENC_Preguntas_ReqA] DEFAULT (0),
[TypeQ] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[RPTA] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Other] [int] NOT NULL CONSTRAINT [DF_ENC_Preguntas_Otra] DEFAULT (0),
[OtherA] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Media] [nvarchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[RND] [int] NULL CONSTRAINT [DF_ENC_Preguntas_RND] DEFAULT (0)
Table ANS:
[IDANS] [bigint] IDENTITY (1, 1) NOT NULL ,
[IDQuestion] [int] NOT NULL ,
[SessionID] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Answer] [nvarchar] (800) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[ANSAOther] [nvarchar] (400) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Finalize] [int] NOT NULL CONSTRAINT [DF_ENC_RPTA_End] DEFAULT (0),
[QTY] [int] NULL CONSTRAINT [DF_ENC_RPTA_QTY] DEFAULT (1)
Felipe
December 13, 2006 at 9:34 am
Off the top of my head, this might give you the desired results, but it also may not be exactly what you are after. The query does use an aggregate function but it is neccessary to do the group by. Don't worry about the MIN unless you expect there to be more than one answer to a given question in a given user session.
The downside is, of course, the hardcoded nature of this code. If you want me to, I'll expand the solution to auto-grow itself.
SELECT SessionID, MIN(CASE WHEN ENC_Questions.Question = 1 THEN ENC_ANS.Answer END) AS Question 1 MIN(CASE WHEN ENC_Questions.Question = 2 THEN ENC_ANS.Answer END) AS Question 2 MIN(CASE WHEN ENC_Questions.Question = 3 THEN ENC_ANS.Answer END) AS Question 3 FROM dbo.ENC_Questions INNER JOIN dbo.ENC_ANS ON dbo.ENC_ANS.IDQuestion = dbo.ENC_AND.IDQuestion WHERE dbo.ENC_Questions.IDSurvey = 2155 ORDER BY dbo.ENC_ANS.SessionID, dbo.ENC_Questions.Order GROUP BY dbo.ENC_ANS.SessionID
SQL guy and Houston Magician
December 13, 2006 at 9:46 am
Robert,
It does not work, as depending on the IDSurvey, the Questions table may have different number of questions, so the pivot column "Question" has to be built dynamic.
Thanks anyway
December 13, 2006 at 9:47 am
I figured.
I'll post a dynamic solution a little later today.
Cheers!
-Robert
SQL guy and Houston Magician
December 13, 2006 at 10:28 am
Thanks,
Looking forward to your reply
FBP
December 13, 2006 at 10:42 am
One more question:
How many questions (roughly) do you expect in an average survey?
In the meantime, I posted a dynamic solution to a similar problem here:
http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=321627#bm321791
SQL guy and Houston Magician
December 13, 2006 at 10:44 am
Not more than 30 I think
December 13, 2006 at 11:02 am
Probably not more than 35
FBP
December 13, 2006 at 3:14 pm
Take Robert's solution as a sample and build the script dynamically.
_____________
Code for TallyGenerator
December 13, 2006 at 5:26 pm
I'm sorry, I'm not going to get a chance to put anything together today. I'm happy to help, just need to find some time.
SQL guy and Houston Magician
December 14, 2006 at 5:04 am
Another solution for this is at
http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=242919#bm243627
Jeff Moden was very generous.
HTH
Dave
Viewing 15 posts - 1 through 15 (of 35 total)
You must be logged in to reply to this topic. Login to reply