Hi ,
In this article we will see how to get a group by comma separated values sql server.
Check Below example ::
step1:: Create table and fill some data in that table.
Create table #Group_by_CommaSeprated(id int ,Hosp_name varchar(50),City varchar(50))insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(1,'sandip','mumbai')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(1,'raj','pune')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(1,'ajit','mumbai')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(1,'aditya','nashik')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(2,'rachit','bhopal')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(2,'rakesh','bihar')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(2,'shrikant','MP')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(2,'chandan','MP')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(3,'dishal','MP')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(3,'saket','UP')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(3,'pranav','sangli')insert into #Group_by_CommaSeprated (id,Hosp_name,City )values(3,'aditya','kolhapur')
step 2:: Check your data .
select * from #Group_by_CommaSeprated
Step 3:: Select Query for get a group by comma separated values sql server.
SELECT id, Hosp_name =STUFF((SELECT ', ' + Hosp_nameFROM #Group_by_CommaSeprated bWHERE b.id = a.idFOR XML PATH('')), 1, 2, ''), City =STUFF((SELECT ', ' + CityFROM #Group_by_CommaSeprated bWHERE b.id = a.idFOR XML PATH('')), 1, 2, '')FROM #Group_by_CommaSeprated aGROUP BY id
Step 4:: OUTPUT
link: http://sandipgsql.blogspot.in/search/label/data%20in%20comma%20separated%20string
0 comments:
Post a Comment