Add up in T-SQL?

If I have data in the following format

 id    subid      text
 1     1          Hello
 1     2          World
 1     3          !
 2     1          B
 2     2          B
 2     3          Q

And I would like in this format:

 id  fold
 1   HelloWorld!
 2   BBQ

How can I execute it in T-SQL?

+3
source share
4 answers

I would highly recommend against this. This is what needs to be handled at your application level.

But ... if you need to:
Combining row values ​​in Transact-SQL

+8
source

temp table and cursor jump to mind ...

Dear Downvoters: the temporary table and cursor should be at least as efficient as the solutions to the recursive query and user functions adopted above. Avoid your fear of cursors; sometimes they are the most effective solution. Sometimes this is the only solution. Talk to him.

EDIT: . , ( ) , , , , ( , ).

, , sql , - " "; . , , , .

--initial data table
create table #tmp (
    id int,
    subid int,
    txt varchar(256)
)

--populate with sample data from original question
insert into #tmp (id,subid,txt) values (1, 1, 'Hello')
insert into #tmp (id,subid,txt) values (1, 2, 'World')
insert into #tmp (id,subid,txt) values (1, 3, '!')
insert into #tmp (id,subid,txt) values (2, 1, 'B')
insert into #tmp (id,subid,txt) values (2, 2, 'B')
insert into #tmp (id,subid,txt) values (2, 3, 'Q')

--temp table for grouping results
create table #tmpgrp (
    id int,
    txt varchar(4000)
)

--cursor for looping through data
declare cur cursor local for
    select id, subid, txt from #tmp order by id, subid

declare @id int
declare @subid int
declare @txt varchar(256)

declare @curid int
declare @curtxt varchar(4000)


open cur

fetch next from cur into @id, @subid, @txt

set @curid = @id
set @curtxt = ''

while @@FETCH_STATUS = 0 begin
    if @curid <> @id begin
        insert into #tmpgrp (id,txt) values (@curid,@curtxt)
        set @curid = @id
        set @curtxt = ''
    end
    set @curtxt = @curtxt + isnull(@txt,'')
    fetch next from cur into @id, @subid, @txt
end

insert into #tmpgrp (id,txt) values (@curid,@curtxt)

close cur

deallocate cur

--show output
select * from #tmpgrp

--drop temp tables
drop table #tmp
drop table #tmpgrp
+1
declare  @tmp table (id int, subid int,txt varchar(256) )  
--populate with sample data from original question 
insert into @tmp (id,subid,txt) values (1, 1, 'Hello') 
insert into @tmp (id,subid,txt) values (1, 2, 'World') 
insert into @tmp (id,subid,txt) values (1, 3, '!') 
insert into @tmp (id,subid,txt) values (2, 1, 'B') 
insert into @tmp (id,subid,txt) values (2, 2, 'B') 
insert into @tmp (id,subid,txt) values (2, 3, 'Q') 

Decision

Select id, fold = (Select cast(txt as varchar(100)) from @tmp t2 where t1.id = t2.id for xml path(''))
from @tmp t1
group by t1.id
+1
source

Wrap this in a function for one execution ...

DECLARE @returnValue varchar(4000)

SELECT @returnValue = ISNULL(@returnValue + ', ' +  myTable.text, myTable.text)
FROM myTable 

RETURN @returnValue

For a small number of records, this will work ... no more than 5 or 10 is too much for the SQL function, and it needs to be moved to the application level, as others have suggested.

0
source

Source: https://habr.com/ru/post/1699380/


All Articles