﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Convert values of mulitiple rows into single column with no duplicate entries / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 21:43:18 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Convert values of mulitiple rows into single column with no duplicate entries</title><link>http://www.sqlservercentral.com/Forums/Topic1385954-392-1.aspx</link><description>thank you very much Sony Francis @EY,Eugene Elutin,dwain.c for your feedback.</description><pubDate>Mon, 19 Nov 2012 21:04:20 GMT</pubDate><dc:creator>sayedkhalid99</dc:creator></item><item><title>RE: Convert values of mulitiple rows into single column with no duplicate entries</title><link>http://www.sqlservercentral.com/Forums/Topic1385954-392-1.aspx</link><description>May be this will help youSELECT a.Province ,a.District ,FPS =  STUFF ( ( SELECT    ','+ b.fpFROM #Result bWHERE a.Province = b.ProvinceAND a.district = b.districtGROUP BY FPORDER BY b.fpFOR XML PATH(''),TYPE ).value('.','VARCHAR(MAX)') , 1,1,SPACE(0))FROM #Result aGROUP BY a.Province,a.district</description><pubDate>Mon, 19 Nov 2012 05:20:55 GMT</pubDate><dc:creator>Sony Francis @EY</dc:creator></item><item><title>RE: Convert values of mulitiple rows into single column with no duplicate entries</title><link>http://www.sqlservercentral.com/Forums/Topic1385954-392-1.aspx</link><description>[quote][b]CELKO (11/18/2012)[/b][hr][quote] I went through the forum and found out a way of concatenating row values into column using [i]stuff[/i], but can't figure out how to remove duplicates and only display them once. [/quote]Why do you wish to destroy and violate the principle of tiered architectures? Why do you want to write proprietary, unmaintainable code?[/quote]I cannot see how concatenating distinct values from rows into single (some-character separated) value is destroying and violating principals of tiered architecture. More than that, when it became available to achieve easily in T-SQL, it was real help in many solutions (client applications and especially - reporting ones). Also, there is absolutely nothing wrong with using proprietary functionality of MS T-SQL. It would be stupid to suggest not to use features of T-SQL which quite often makes it stand out from another RDBMS solutions. Would you ask Oracle developers not to use Oracle specific features/libraries?Actually, if you so against "proprietary" coding, what do you think in coding practices generally? No C#, VB.NET as it's MS proprietary, no Java as it's IBM one... Looks like every language (eg. assembler), in certain extend is a proprietary...Why do you think that there is a real need of porting every single solution from one language/system base to another? How often does it happen in real life? Is it use of proprietary features, is the main issue when you port an application?  My answers to above three questions would be:1. No much need!2. Not very often, definitely less often then replacing system completely.3. No, it's clearly not. Replacing ISNULL with COALESCE, for example is nothing in comparison with issues related with use of new drivers, OS architecture, security and many other... And about "unmaintainable code". The technique used by OP is well known among T-SQL professionals and it's one of the best and used methods to achieve what OP needs. How does it compromise maintainability? If this block of code is appropriately commented and nicely formatted (for ease of read), it will not effect code maintainability in any manner.</description><pubDate>Mon, 19 Nov 2012 03:25:45 GMT</pubDate><dc:creator>Eugene Elutin</dc:creator></item><item><title>RE: Convert values of mulitiple rows into single column with no duplicate entries</title><link>http://www.sqlservercentral.com/Forums/Topic1385954-392-1.aspx</link><description>[quote][b]CELKO (11/18/2012)[/b][hr]Why do you want to write proprietary, unmaintainable code?[/quote]Job security?</description><pubDate>Sun, 18 Nov 2012 20:19:43 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Convert values of mulitiple rows into single column with no duplicate entries</title><link>http://www.sqlservercentral.com/Forums/Topic1385954-392-1.aspx</link><description>[quote] I went through the forum and found out a way of concatenating row values into column using [i]stuff[/i], but can't figure out how to remove duplicates and only display them once. [/quote]Why do you wish to destroy and violate the principle of tiered architectures? Why do you want to write proprietary, unmaintainable code?</description><pubDate>Sun, 18 Nov 2012 20:17:47 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Convert values of mulitiple rows into single column with no duplicate entries</title><link>http://www.sqlservercentral.com/Forums/Topic1385954-392-1.aspx</link><description>Here's another way to do it while avoiding the use of DISTINCT:[code="sql"]SELECT Province, District    ,FPS=STUFF((        SELECT ',' + b.FP        FROM #Result b        WHERE a.Province = b.Province AND a.District = b.District        GROUP BY Province, District, FP        FOR XML PATH('')), 1, 1, '')FROM #Result aGROUP BY Province, District[/code]</description><pubDate>Sun, 18 Nov 2012 17:55:04 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Convert values of mulitiple rows into single column with no duplicate entries</title><link>http://www.sqlservercentral.com/Forums/Topic1385954-392-1.aspx</link><description>the solution is to convert the column to varchar and run that inside subquery, hope it will help some one else with similar problem, now one more thing here left is the order of entries.SELECT a.Province ,a.District ,      FPS =            (select stuff((SELECT distinct ',' + cast(fp as varchar(10))           FROM #Result t2           where t2.Province = a.province and            t2.district=a.district           FOR XML PATH('')),1,1,'')) FROM #Result aGROUP BY a.Province,a.district</description><pubDate>Sat, 17 Nov 2012 11:48:45 GMT</pubDate><dc:creator>sayedkhalid99</dc:creator></item><item><title>Convert values of mulitiple rows into single column with no duplicate entries</title><link>http://www.sqlservercentral.com/Forums/Topic1385954-392-1.aspx</link><description>hi all,i went through the forum and found out a way of concatenating row values into column using stuff, but can't figure out how to remove duplicates and only display them once.if you have any function so i could use that with more columns rather then using this stuff function.IF OBJECT_ID('TempDB..#Result','U') IS NOT NULLDROP TABLE #Resultcreate Table #Result (Province varchar(100),District varchar(100),Fp varchar(10),Cycle int)insert into #Result select 'LOGAR','Charkh','Abc',1 union ALLselect 'LOGAR','Charkh','Dacaar',2 union ALLselect 'LOGAR','Charkh','Ze',3 union ALLselect 'LOGAR','Charkh','ARTs',4 union ALLselect 'LOGAR','Charkh','Abc',5 SELECT a.Province ,a.District ,      FPS =               STUFF ( ( SELECT ','+  b.fp						FROM #Result b						WHERE a.Province = b.Province						 AND  a.district = b.district						ORDER BY b.fp						FOR XML PATH(''),TYPE 					   ).value('.','VARCHAR(MAX)') 					  , 1,1,SPACE(0))FROM #Result aGROUP BY a.Province,a.district-- Require Output with out duplicates fps select 'LOGAR' as Province,'Charkh' as District,'Abc,ARTs,Dacaar,Ze' as FPSthanks,</description><pubDate>Sat, 17 Nov 2012 10:42:35 GMT</pubDate><dc:creator>sayedkhalid99</dc:creator></item></channel></rss>