Connecting Tech Pros Worldwide Help | Site Map

Concatenating Row Values into 1 Record/String

  #1  
Old June 29th, 2009, 01:34 PM
Member
 
Join Date: Apr 2007
Location: india
Posts: 110
hi friends

i have 3 table

table 1 - > question master
field qid (primary key), question (nvarchar) type (1- single answer, 2-multiple answer)
qid - question - type
1 - 5+3 - 1
2 - 9+1 - 1
3 - 11x11 - 1
4 - ques1 - 2

table 2 multiplechoiceanswer
field MCid (primary key) , qid, answers
MCid - qid - answers
1 - 1 - 10
2 - 1 - 9
3 - 1 - 8
4 - 1 - none
5 - 2 - 10
6 - 2 - 11
7 - 2 - 12
8 - 2 - 13
(each question have 4 choice)

table 3 correctanswer
field cid(p) qid mcid
cid - qid - mcid
1 - 1 - 3
2 - 2 - 5
3 - 3 - 1
4 - 3 - 3
5 - 10 - 1
6 - 10 - 2
7 - 10 - 3

(some question answer more than 1 choice also applicable)


i want this type output format
question - multiple_choice_answer - correct_answer
5+3 - (a)10 (b)9 (c)8 (d)none - 8
9+1 - (a)10 (b)11 (c) 12 (d) 13 - 10
question - (a)ans1 (b)ans2 (c)ans3 (d) ans4 - ans1- ans2- ans3 (multiple answer)

pls help
  #2  
Old June 30th, 2009, 07:48 AM
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,914
Provided Answers: 1

re: Concatenating Row Values into 1 Record/String


Convert it to a delimited string. Read this, then process how it would look in the front-end.

Good luck

--- CK
  #3  
Old June 30th, 2009, 11:09 AM
Member
 
Join Date: Apr 2007
Location: india
Posts: 110

re: Concatenating Row Values into 1 Record/String


hi

thanks for ur reply it is useful
i have one doubt

this is my code


declare @DelimitedString varchar(5000)
select tblquestion.questionname,(SELECT @DelimitedString = COALESCE(@DelimitedString+',' , '') + answers FROM tblanswer where tblanswer.questionid=que.questionid) as answers from tblquestion que

here i got one error mess

Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near '='.

my sql statement is correct?
it not pls guide me

thanks
  #4  
Old June 30th, 2009, 09:14 PM
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,914
Provided Answers: 1

re: Concatenating Row Values into 1 Record/String


try:

Expand|Select|Wrap|Line Numbers
  1. declare @DelimitedString varchar(5000)
  2. SELECT @DelimitedString = COALESCE(@DelimitedString+',' , '') + answers FROM tblanswer a
  3. inner join questionmaster m on m.qid = 1 and m.qid = a.qid 
  4.  
  5. print @DelimitedString 
  6.  

Happy Coding!!

-- CK
  #5  
Old July 8th, 2009, 08:01 PM
Member
 
Join Date: Apr 2007
Location: india
Posts: 110

re: Concatenating Row Values into 1 Record/String


thanks for ur reply
here your using m.qid = 1 know
so my result based on question id =1 but i want all (ie) i want all question id

pls help
  #6  
Old July 8th, 2009, 08:38 PM
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,914
Provided Answers: 1

re: Concatenating Row Values into 1 Record/String


Remove that filter

--- CK
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can you do this with PHP? art@unsu.com answers 9 June 2nd, 2008 11:37 AM
plpgsql-fct. fails on NULL in record variables Daniel Martini answers 1 November 23rd, 2005 02:18 AM
Combine field in several recs summarized into one record? mark answers 8 November 12th, 2005 04:29 PM
Fast Way to Concat Records to Comma Sep VARCHAR Jeremy Pridmore answers 11 July 20th, 2005 03:38 AM