Click here to Skip to main content
15,508,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi


I have a datatable with 5 columns i have to check whether the ID field in datatable is duplicate if so i have to find the value of column name and append it in the first row so tht multiple rows can be avoided

ID Name Age
1 sadf 2
1 sadf 4

i have to show like this

ID Name Age
1 sadf 2,4
Posted
Comments
Swinkaran 17-Sep-12 0:54am    
What is the purpose of doing this? This is not a correct approach.
Arjun Menon U.K 17-Sep-12 1:05am    
I am trying to list a product (say a Bike) i have to list among them the manufacturer the model and the service center..Only the service center name changes where as it remains the same for the manufacturer and model. So i should display list as <manufacturer> <model> <servicecenter1>,<servicecenter2>

This is not a good design and makes querying data difficult and defeats the purpose of a relational database.
 
Share this answer
 
Comments
Arjun Menon U.K 17-Sep-12 0:52am    
Its the requirement other wise i will have duplicate values except for the Age columns
Hey there,

As a developer, you MUST improve your Google-arts :) Check out the below solution, it should help you.

A modified solution from: Common Solutions for T-SQL[^]
SQL
SELECT
   YT.ID,
   AgeList = substring((SELECT ( ', ' + RTRIM(LTRIM(STR(YT2.ItemId))))
                           FROM YourTable YT2
                           WHERE YT.ID = YT2.ID
                           ORDER BY 
                              YT2.ID,
                              YT2.Age
                           FOR XML PATH( '' )
                          ), 3, 5000)FROM YourTable YT

Hope this helps, regards
 
Share this answer
 
Comments
Arjun Menon U.K 17-Sep-12 1:52am    
Thanks CodeHawkz:) i did in c# anyways thanks a lot :)
Hi,

See the similar discussion Here[^]. It may work for you.
 
Share this answer
 
Comments
Arjun Menon U.K 17-Sep-12 1:52am    
Thanks amit :) i did in c# anyways thanks a lot :)
AmitGajjar 17-Sep-12 1:54am    
Nice, You can use LINQ to do that from C#. but i suggest you from SQL.
Arjun Menon U.K 17-Sep-12 8:10am    
@amit i avoided LING just mere looping and checking... :)
AmitGajjar 17-Sep-12 8:11am    
ok... good.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900