Author |
Topic |
tdave
Starting Member
3 Posts |
Posted - 2009-02-13 : 13:36:21
|
i m getting results in vertical line and i want them in horizontal line...how can i get those..i m getting like:friendsother textheartdue to reasonstatei want friends other text heart due to reason statehow can i get itmy sql statement is select * from sales (in that one column is salespart which iwant results in horizontal line)thank you! |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-13 : 13:40:27
|
[code]DECLARE @result varchar(8000)SELECT @result=coalesce(@result + ' ','') + youcolFROM YourtableSELECT @result[/code] |
|
|
tdave
Starting Member
3 Posts |
Posted - 2009-02-13 : 13:56:42
|
i have fname, lname, organization, type and date in one tableso select fname,lname,org,type,date from table1 and type is the column nameexamplefname,lname,org,type,date ---------------------------abc xyz d001 friends 2/1/2009abc xyz d001 other text 2/1/2009abc xyz d001 heart 2/1/2009abc xyz d001 due to reason 2/1/2009hnd jhj d002 statewise 2/10/2009hnd jhj d002 countrywise 2/10/2009so i want results asfname,lname,org,type, typeother, date ---------------------------abc xyz d001 friends other text,heart,due to reason 2/1/2009hnd jhj d002 statewise countrywise 2/10/2009so the vertical values will be in another column - named type other.is that possible?? |
|
|
tdave
Starting Member
3 Posts |
Posted - 2009-02-13 : 15:04:59
|
i got all values friends,other text,heart,due to reason,statewise,countrywise all in one columnDECLARE @t varchar(4000)SET @t = ''SELECT @t = @t + ', ' + type From table1SELECT STUFF(@t,1,2,'')but i want only first value in one column and rest seperating by coma in another column.fname,lname,org,type, typeother, date ---------------------------abc xyz d001 friends other text,heart,due to reason 2/1/2009hnd jhj d002 statewise countrywise 2/10/2009do u have any idea? |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-13 : 16:22:19
|
Please see this:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=119518 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-14 : 02:15:02
|
quote: Originally posted by tdave i got all values friends,other text,heart,due to reason,statewise,countrywise all in one columnDECLARE @t varchar(4000)SET @t = ''SELECT @t = @t + ', ' + type From table1SELECT STUFF(@t,1,2,'')but i want only first value in one column and rest seperating by coma in another column.fname,lname,org,type, typeother, date ---------------------------abc xyz d001 friends other text,heart,due to reason 2/1/2009hnd jhj d002 statewise countrywise 2/10/2009do u have any idea?
there's not concept of first or last unless you specify this based on another unique valued column. do you have any other unique valued column (like an identity) in your table? |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-14 : 02:41:27
|
try like thisdeclare @temp table (fname VARCHAR(32),lname VARCHAR(32),org VARCHAR(32),type VARCHAR(32),date vARCHAR(32))insert into @tempselect 'abc' ,'xyz' ,'d001' ,'friends', '2/1/2009' union allselect 'abc' ,'xyz' ,'d001' ,'other text', '2/1/2009' union allselect 'abc' ,'xyz' ,'d001' ,'heart', '2/1/2009' union allselect 'abc' ,'xyz' ,'d001' ,'due to reason', '2/1/2009' union allselect 'hnd' ,'jhj' ,'d002' ,'statewise', '2/10/2009' union allselect 'hnd' ,'jhj' ,'d002' ,'countrywise', '2/10/2009'Select identity(int,1,1)as rid , * into #temp from @tempselect *,(Select Count(*) from #temp Where fname = Z.fname and rid <= Z.rid ) as Seq into #temp1 from #temp zselect * from #temp1DECLARE @table TABLE ( namefirst varchar(32),Name VARCHAR(MAX),id1 varchar(32))DECLARE @str VARCHAR(MAX),@id1 varchar(32),@str1 varchar(32)SELECT @str = '',@str1=''SELECT @ID1 = MIN(org) FROM #temp1 WHILE(@ID1 IS NOT NULL )BEGIN SELECT @str1 = type FROM #temp1 WHERE @id1= org and seq = 1 SELECT @STR = @STR + ','+ type FROM #temp1 WHERE @id1= org and seq <> 1 SELECT @str = STUFF(@str , 1 , 1,'') INSERT INTO @TABLE SELECT @str1,@STR,@id1 SELECT @ID1 = MIN(org) FROM #temp1 WHERE org>@ID1 SELECT @str = '',@str1=''ENDSELECT namefirst,id1,name FROM @table-- works in 2005 & aboveselect distinct fname,lname,org,substring((stuff((select ','+type from @temp t where t.fname = tt.fname for xml path ('')),1,1,'')),1,charindex(',',(stuff((select ','+type from @temp t where t.fname = tt.fname for xml path ('')),1,1,'')))-1) as type,(stuff((select ','+type from @temp t where t.fname = tt.fname for xml path ('')),1,charindex(',',(stuff((select ','+type from @temp t where t.fname = tt.fname for xml path ('')),1,1,'')))+1,'')) as typeother,date from @temp tt |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-14 : 04:01:12
|
Try this also,declare @temp table (fname VARCHAR(32),lname VARCHAR(32),org VARCHAR(32),type VARCHAR(32),date vARCHAR(32))insert into @tempselect 'abc' ,'xyz' ,'d001' ,'friends', '2/1/2009' union allselect 'abc' ,'xyz' ,'d001' ,'other text', '2/1/2009' union allselect 'abc' ,'xyz' ,'d001' ,'heart', '2/1/2009' union allselect 'abc' ,'xyz' ,'d001' ,'due to reason', '2/1/2009' union allselect 'hnd' ,'jhj' ,'d002' ,'statewise', '2/10/2009' union allselect 'hnd' ,'jhj' ,'d002' ,'countrywise', '2/10/2009'declare @tempTable table ( id VARCHAR(32),string varchar(64)) Declare @str VARCHAR(Max) , @str1 VARCHAR(Max),@str2 varchar(32),@str3 varchar(32) Select @str = '',@str1 = '',@str2 = '',@str3 = '' Select @str = @str + ',' + type from @temp where fname in( 'abc') Select @str1 = type From @temp Where fname = 'abc' Select @str2 = @str2 + ',' + type from @temp where fname in( 'hnd') Select @str3 = type From @temp Where fname = 'hnd' Set @str = substring(@str,charindex(',',@str)+1,len(@str)) set @str2 = Substring(@str2,charindex(',',@str2)+1,len(@str2))Insert into @temptable select 'abc' as Id,@str union all select 'hnd' as Id,@str2 select distinct t.id,t1.lname,t1.org,substring(t.string,1,charindex(',',string)-1) as type,substring(t.string,charindex(',',string)+1,len(string)) as TypeOther,t1.date from @temptable tinner join @temp t1 on t1.fname = t.id |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-14 : 04:02:55
|
quote: Originally posted by bklr try like thisdeclare @temp table (fname VARCHAR(32),lname VARCHAR(32),org VARCHAR(32),type VARCHAR(32),date vARCHAR(32))insert into @tempselect 'abc' ,'xyz' ,'d001' ,'friends', '2/1/2009' union allselect 'abc' ,'xyz' ,'d001' ,'other text', '2/1/2009' union allselect 'abc' ,'xyz' ,'d001' ,'heart', '2/1/2009' union allselect 'abc' ,'xyz' ,'d001' ,'due to reason', '2/1/2009' union allselect 'hnd' ,'jhj' ,'d002' ,'statewise', '2/10/2009' union allselect 'hnd' ,'jhj' ,'d002' ,'countrywise', '2/10/2009'Select identity(int,1,1)as rid , * into #temp from @tempselect *,(Select Count(*) from #temp Where fname = Z.fname and rid <= Z.rid ) as Seq into #temp1 from #temp zselect * from #temp1DECLARE @table TABLE ( namefirst varchar(32),Name VARCHAR(MAX),id1 varchar(32))DECLARE @str VARCHAR(MAX),@id1 varchar(32),@str1 varchar(32)SELECT @str = '',@str1=''SELECT @ID1 = MIN(org) FROM #temp1 WHILE(@ID1 IS NOT NULL )BEGIN SELECT @str1 = type FROM #temp1 WHERE @id1= org and seq = 1 SELECT @STR = @STR + ','+ type FROM #temp1 WHERE @id1= org and seq <> 1 SELECT @str = STUFF(@str , 1 , 1,'') INSERT INTO @TABLE SELECT @str1,@STR,@id1 SELECT @ID1 = MIN(org) FROM #temp1 WHERE org>@ID1 SELECT @str = '',@str1=''ENDSELECT namefirst,id1,name FROM @table-- works in 2005 & aboveselect distinct fname,lname,org,substring((stuff((select ','+type from @temp t where t.fname = tt.fname for xml path ('')),1,1,'')),1,charindex(',',(stuff((select ','+type from @temp t where t.fname = tt.fname for xml path ('')),1,1,'')))-1) as type,(stuff((select ','+type from @temp t where t.fname = tt.fname for xml path ('')),1,charindex(',',(stuff((select ','+type from @temp t where t.fname = tt.fname for xml path ('')),1,1,'')))+1,'')) as typeother,date from @temp tt
you cant guarantee that identity value will be generated in correct order by SELECT INTO ...statementhttp://support.microsoft.com/kb/273586 |
|
|
|
|
|