how to get single column name from database in sql? [Resolved]

Posted by Hariinakoti under Sql Server on 11/27/2012 | Points: 10 | Views : 1174 | Status : [Member] | Replies : 2
Hi All,
I saved Values like this

1 AndhraPradesh 22 Vizianagaram Denkada
1 AndhraPradesh 22 Vizianagaram Bhoghapuram
1 AndhraPradesh 22 Vizianagaram Pusapatirega
1 AndhraPradesh 21 Visakhapatnam Bheemunipatnam
1 AndhraPradesh 21 Visakhapatnam Araku Valley
1 AndhraPradesh 21 Visakhapatnam Anakapalle
1 AndhraPradesh 21 Visakhapatnam Gajuwaka
1 AndhraPradesh 21 Visakhapatnam Chodavaram


But i want output like this
StateID State CityID City Mandal
1 AndhraPradesh 22 Vizianagaram Denkada
Bhogapuram
Pusapatirega
21 Visakhapatnam Araku Valley
Gajuwaka
2 TamilNadu 1 Chennai xxxxxxxx
2 Madurai yyyyyyyy


is it possible r not?
can any one solve it plz

Thanks & Regards
Hari



Responses

Posted by: Pandians on: 11/27/2012 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Check It Out!
Create Table TableName

(
Id Int,
Column1 Varchar(50),
Column2 int,
Column3 Varchar(50),
Column4 Varchar(50)
)
Go

Insert TableName Values(1,'AndhraPradesh',22,'Vizianagaram','Denkada')
Insert TableName Values(1,'AndhraPradesh',22,'Vizianagaram','Bhoghapuram')
Insert TableName Values(1,'AndhraPradesh',22,'Vizianagaram','Pusapatirega')
Insert TableName Values(1,'AndhraPradesh',21,'Visakhapatnam','Bheemunipatnam')
Insert TableName Values(1,'AndhraPradesh',21,'Visakhapatnam','Araku Valley')
Insert TableName Values(1,'AndhraPradesh',21,'Visakhapatnam','Anakapalle')
Insert TableName Values(1,'AndhraPradesh',21,'Visakhapatnam','Gajuwaka')
Insert TableName Values(1,'AndhraPradesh',21,'Visakhapatnam','Chodavaram')
Go

Select Distinct o.Id, o.Column1, o.Column2, o.Column3,
(
Select I.Column4 + ', ' From TableName I
Where I.id = o.id and I.column1 = o.column1 and I.column2 = o.column2 and I.column3 = o.column3
For XML Path('')
) [Combined Data]
from TableName O
Go


Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Krv on: 11/27/2012 [Member] Starter | Points: 25

Up
0
Down
Sorry, unable to understand your expected output, can u pls format it in a tablur form and attach the image

Thanks,
R.Vasanth
MCTS(SQL Server & BI)
http://bigdatatech.blogspot.com

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response