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
Explain what are the restrictions that views have to follow? : SQL Server Architecture
Explain the cursor lock types?
What are the types of joins in sql?
Mention the different types of triggers?
What is exclusive locks?
How many database files are there in sql server 2000?what are they?
What are number line correlation administrators will use while working with a subquery?
How to create user defined functions with parameters?
explain different types of backups avaialabe in sql server? : Sql server database administration
What are the tool windows in sql server management studio? : sql server management studio
What is the difference between ddl,dml and dcl commands?
How many null values we can have in a unique key field in sql server?
How many types of attribute relationships are there? : sql server analysis services, ssas
Do you know what are the differences between lost updates and uncommitted dependencies?
can an order by clause be used in a creation of a view?