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 / prakash
CREATE TABLE CITY (CITYID INT, ContinuationID INT, CITYNAME
VARCHAR(50))
GO
INSERT CITY
SELECT 1 ,1, 'SAN'
UNION ALL
SELECT 1, 2, 'DIEGO'
UNION ALL
SELECT 2, 1, 'SAN'
UNION ALL
SELECT 2, 2, 'FRANCISCO'
UNION ALL
SELECT 3, 1, 'CHICAGO'
UNION ALL
SELECT 4, 1, 'NEW'
UNION ALL
SELECT 4, 2, 'YORK'
UNION ALL
SELECT 4, 3, 'CITY'
SELECT cityid,
max(CASE P.ContinuationID WHEN 1 THEN
P.CITYNAME ELSE '' END) AS Name1,
max(CASE P.ContinuationID WHEN 2 THEN
P.CITYNAME ELSE '' END) AS Name2,
max(CASE P.ContinuationID WHEN 3 THEN
P.CITYNAME ELSE '' END) AS Name3
FROM CITY AS P
GROUP BY P.cityid
| Is This Answer Correct ? | 8 Yes | 0 No |
Post New Answer View All Answers
Write down the syntax and an example for create, rename and delete index?
Explain the difference between cross join and full outer join?
What is global temp table?
What is a functions and types in sql server?
Difference between 2NF &3NF ?
Tell me what is difference between view and materialized view?
How to define and use table alias names in ms sql server?
What is Dependency Injection and provide example?
What is the difference between sql server 2000 object owner and sql server 2005 schema? : sql server database administration
how can a database be repaired? : Sql server administration
Define left outer join?
Explain the disadvantages/limitation of the cursor?
How to add a new dsn with the odbc driver for sql server?
List down some advantages of sql stored procedure?
What is format parameter in ssrs?