Saturday, 24 August 2013

Nested Single Query for concatenation of specific column in multiple row in single column

Nested Single Query for concatenation of specific column in multiple row
in single column

i have currently working on sql server and writing a stored procedure to
retrieve some data. My requirement is as follows:
Table A:PersonId,FirstName,LastName,Address,CourseId
(Primary Key For Table B,Foreign Key Here)
Table B:CourseDescription,CourseId
now for each course there might be multiple student enrolled to that
course.my requirement is to concatenate each student LastName,FirstName.if
there are 3 or more student in a course we have to set another flag value
as 'Y' in the result.I've done with Stored proc using temp table , and
updating it step by step. my stored proc algo is like this: temp table has
column:
SeqId,CourseId,CourseDescription,StudentNameConcat,IsMoreThan3
First i update course id,description.then from this table i looped based
on sequence id(SeqId) and retrieve list of student name as column value
and concat it in declared variable.
This approach is not good because it is not a set based approach.and i
believe there must be an alternative approach for it in a single query
using inner query or loop join.I'm still reading and trying to implement
it in a single query.But still not getting any clue.

No comments:

Post a Comment