Author |
Topic |
hurstgroup
Starting Member
2 Posts |
Posted - 2009-07-16 : 12:17:23
|
I have two rows in a table which I need to return as a single row as they refer to the same item. The problem is that there is a field TYPE which has a different value in each. One will have Text and one HTML. I would like to concatenate these into a new field in the output but I cannot see how to do it!It maybe that only one of the rows may exist.This is example source dataSOURCE,TYPE,DESCRIPTIONa111,html,Product1a111,Text,Product1I can get the resulta111,Product1easily but I really wantA111,HTML/Text,Product1Is this possible?ThanksRobert. |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-07-16 : 13:50:54
|
Try this...SELECT STUFF((SELECT '/' + [TYPE] FROM @t WHERE SOURCE = t.SOURCE AND [DESCRIPTION] = t.[DESCRIPTION] FOR XML PATH('')),1,1,'') AS [newcol], t.SOURCE, t.[DESCRIPTION] FROM @t t GROUP BY [DESCRIPTION], SOURCE |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-16 : 13:51:48
|
yup with a UDFcreate a UDF asCREATE FUNCTION GetData(@Source varchar(5),@Description varchar(20))RETURNS varchar(8000)ASBEGINDECLARE @RetVal varchar(8000)SELECT @RetVal=COALESCE(@RetVal+'/','') + TYPEFROM YourTableWHERE SOURCE=@SourceAND DESCRIPTION=@DescriptionRETURN @RetValENDthen use it asSELECT DISTINCT SOURCE,dbo.GetData(SOURCE,DESCRIPTION) AS TypeList,DESCRIPTIONFROM YourTable |
|
|
hurstgroup
Starting Member
2 Posts |
Posted - 2009-07-16 : 16:56:01
|
Hi,Thanks for the replies. The only problem now is 1) I am using SQL 2000 which I am not convinved supports some of these features and 2) my SQL is too poor to fully grasp what is going on.This is the exact statement which I would use to get the distinct values - can you please re-write to work with this single statement idea (ideally as I need to do this from an App).select source, descript_lfrom orderresponsegroup by source,descript_lThe field which is different is Type and has a value of HTML or Text.Sorry but I am struggling a bit on this yet it seems so close now I have a solution.Robert. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-07-16 : 17:31:11
|
This modified solution (from visakh) should work in sql server 2000 too:CREATE FUNCTION GetData(@Source varchar(5),@Description varchar(20))RETURNS varchar(8000)ASBEGINDECLARE @RetVal varchar(8000)--SELECT @RetVal=COALESCE(@RetVal+'/','') + TYPESELECT @RetVal=isnull(@RetVal,'')+case when @RetVal is null then '' else '/' end + TYPEFROM tab1WHERE SOURCE=@SourceAND [DESCRIPTION]=@DescriptionRETURN @RetValENDgocreate table tab1(source varchar(255),[type] varchar(255),[description] varchar(255))insert tab1select 'a111','html','Product1' union allselect 'a111','Text','Product1'--I can get the result----a111,Product1----easily but I really want----A111,HTML/Text,Product1select * from tab1SELECT DISTINCT SOURCE,dbo.GetData(SOURCE,[DESCRIPTION]) AS TypeList,[DESCRIPTION]FROM tab1drop table tab1drop function getdata No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-07-16 : 18:12:12
|
This is a SQL Server 2008 forum.Moderator, please move to a SQL Server 2000 forum. N 56°04'39.26"E 12°55'05.63" |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2009-07-16 : 18:13:21
|
moved.___________________________________________________________________________Causing trouble since 1980Blog: http://weblogs.sqlteam.com/mladenpSpeed up SSMS development: www.ssmstoolspack.com <- version 1.5 out! |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-07-16 : 18:15:23
|
wow - that was fast! No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-07-16 : 18:16:36
|
I've said it before. Spirit must be a Microsoft Mole :-) N 56°04'39.26"E 12°55'05.63" |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-07-16 : 18:17:47
|
Ah yes I remember.. No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
|