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
What are the steps to follow to configure SQL*Net?
How to provide values to user defined function parameters?
how to use DTS package in 2000,2005,2008 in sql server
what is checksum in sql server.........???
Explain mixed authentication mode of sql server?
How do you delete a data source?
Explain about system database?
You accidentally delete the msdb database what effect does this have on your existing sql databases, and how do you recover?
Is mysql better than sql server?
How each E-R model constructs can be mapped to the relational model?
Explain linked server in sql?
What does it mean to manipulate data?
What are the components of sql server service broker?
Explain about merge replications?
Explain activity monitors