Hi all,

I need query help for below senorio, could you please help
me.

TableName = City
CITYID ContinuationID CITYNAME
1 1 SAN
1 2 DIEGO
2 1 SAN
2 2 FRANCISCO
3 1 CHICAGO
4 1 NEW
4 2 YORK
4 3 CITY

Could you please help me to write a generalized SQL that
returns results as given below in the

Query result
CITYID NAME1 NAME2 NAME3 NAME4 NAME5
1 SAN DIEGO
2 SAN FRANCISCO
3 CHICAGO
4 NEW YORK CITY

Answer Posted / sandeep modapathi

Hi all
Gopi Muluka is right... but here I'm posting one different
thing.. ie the out put will be like this

Only two columns

It uses a function for concatination

CityID CityName
1 SAN DIEGO
2 SAN FRANCISCO
3 CHICAGO
4 NEW YORK CITY


CREATE FUNCTION DBO.GROUP_CONCAT(@CITYID VARCHAR(100))
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @STRING VARCHAR(8000)
SET @STRING=''
SELECT @STRING=@STRING+' ' +CITYNAME FROM CITY
WHERE CITYID=@CITYID ORDER BY CONTINUATIONID
RETURN LTRIM(@STRING)
END

select distinct CITYID,dbo.GROUP_CONCAT(CITYID)as CityName
from city

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is indexing in sql server with example?

697


List the different types of collation sensitivities in sql server?

729


Can we deploy SSRS reports on our personal website?

148


Do you know what are the properties of the relational tables?

790


wat wil hapn if we give the both read and deny read permission to user?

1864






Write a program to fetch first 10 records from a file?

759


What is precedence constraint?

717


tell me the disaster recovery plan

2194


What are the types of joins in sql?

776


Write a query to include a constraint, to check whether the employee salary is greater than 5000?

1347


How do I setup a sql server database?

763


Describe the functionalities that views support.

847


Do you know spatial data types - geometry and geography in sql server 2008?

768


What is the difference between a local and a global temporary table?

821


How to find table changes in sql server?

827