Click here to Skip to main content
Sign Up to vote bad
good
See more: SQL2008
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
 
1 AndhraPradesh 22 Vizianagaram Denkada
Bhogapuram
Pusapatirega
21 Visakhapatnam Araku Valley
Gajuwaka
 
is it possible r not?
can any one solve it plz
Thanks & Regards
Hari
Posted 27 Nov '12 - 1:07
Edited 27 Nov '12 - 2:13

Comments
Krunal R - 27 Nov '12 - 8:04
What have you tried ? And you want column name or mentioned value ??

2 solutions

This is my solution:
 
Select Distinct o.State, o.City,
 
(
 
Select I.Mandal + ', ' From Mandal I
 
Where I.StateID = o.StateID and I.CityID = o.CityID
 
For XML Path('')
 
) [Combined Data]
 
from Country O
  Permalink  
Based upon your illustration, I think the answer you are looking for is a qualified "sort of". What it looks like you want is a new row with just the last column's data when the first part is duplicated. Sure, you could blank the other columns out but what's the point. Besides, the SQL statement to do so would be ugly. However, if you want all of the values for that last column to be in one column (so each person gets one row and the last column has n number of entries in it), you can do so using a CROSS APPLY statement. Here is a forum post that will help you out:
 
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=102429[^]
 
The statement from that post that would probably be what you want is this one:
SELECT p.ASSIGNNUM,p.DESC,p.STARTDATE,LEFT(el.EmpList,LEN(el.EmpList)-1)
FROM #TableParent p
CROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()]
             FROM #TableChild
             WHERE ASSIGNNUM =p.ASSIGNNUM 
             FOR XML PATH('')) el(EmpList)
Obviously you would need to modify it to fit your needs but the end result would be a comma-delimited list of values in the last column.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 496
1 Mahesh Bailwal 342
2 Maciej Los 228
3 Aarti Meswania 215
4 Arun Vasu 174
0 Sergey Alexandrovich Kryukov 9,607
1 OriginalGriff 7,214
2 CPallini 3,943
3 Rohan Leuva 3,261
4 Maciej Los 2,758


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid