Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Below is the table t1 in SQL with column like this, srno is the sequence just use to move cursor.

acctnum(c1)    note (c2)   srno
12             abc          1
               efd          2
               xyz          3
15             pqr          4
16             uio          5
               njk          6
               uuuwr        7
               kmjkj        8
18             zzzzzzz      9


I need the result like as below mention, it should be in single record

acctnum(c1)    note (c2)
12                  abc efd xyz
15                  pqr
16                  uio njk uuwr kmjkj
18                  zzzzzzzzz


What I have tried:

I have tried below cursor query to fetch it but it is not wokring properly as it is not fetching few records of note for few acctnum. Please help me to correct the code and get proper single record as per accountnumber.

SQL
SET NOCOUNT ON;
 
declare @accounttNumber varchar(100);
declare @note varchar(max);
declare @SrNo int;
declare @FinalNote varchar(max);
 
declare c1 cursor for 
SELECT [ACCTNUM],[note],[srno] FROM lk_tmp_data
 
open c1;
 
fetch next from c1 into @accounttNumber,@note,@SrNo;
 
WHILE @@FETCH_STATUS = 0
begin
	
	if (@SrNo!='')
	begin
	if (@accounttNumber!='')
	Begin
		print 'TicketNumber: '+@accounttNumber+' FinalNote : ' + @FinalNote;
		set @FinalNote = '';
	End
	--else 
		set	@FinalNote= @FinalNote + ' ' + @note+' ';
	end;
	--SET @SQLQuery ='';
	--EXEC (@SQLQuery)
	fetch next from c1 into @accounttNumber,@note,@SrNo;
end;
 
CLOSE c1;
DEALLOCATE c1;
Posted
Updated 10-Nov-23 1:14am
v2
Comments
CHill60 10-Nov-23 8:06am    
Rarely, if ever, do you need to use any kind of loop in SQL - it is set-based. See my article Processing Loops in SQL Server[^]
CHill60 10-Nov-23 8:08am    
Does the table really look like that?
acctnum(c1)    note (c2)   srno
12             abc          1
               efd          2
               xyz          3
15             pqr          4...etc
or does it look like
acctnum(c1)    note (c2)   srno
12             abc          1
12             efd          2
12             xyz          3
15             pqr          4...etc
Because if it doesn't look like the 2nd example it will be incredibly difficult to get your data back again
Member 11776570 10-Nov-23 9:02am    
like this
Dave Kreskowiak 10-Nov-23 9:28am    
I guarantee you your table doesn't look like that unless, UNLESS, you're storing data in string formats, making it neigh impossible to do any actual processing on it.

You haven't specified which version of SQL Server you're using. Assuming 2017 or later, the simplest solution is to use STRING_AGG[^]:
SQL
SELECT
    acctnum,
    STRING_AGG(note, ' ') WITHIN GROUP (ORDER BY srno) As note
FROM
    YourTable
GROUP BY
    acctnum
;
If you're using an older version that doesn't support STRING_AGG, you'll need to use one of the alternative methods described here:
Concatenating Row Values in Transact-SQL - Simple Talk[^]
 
Share this answer
 
Comments
Maciej Los 10-Nov-23 9:10am    
5ed!
There's few ways to achieve that.
I'd like to provide a sample with Common Table Expressions:

SQL
;WITH CTE AS
(
  SELECT 1 Lvl, acctnum, note, srno, note newnote
  FROM Table1
  WHERE acctnum IS NOT NULL
  UNION ALL
  SELECT C.Lvl +1, C.acctnum, T.note, T.srno, CONCAT(C.newnote, ' ', T.note) newnote
  FROM CTE C
  INNER JOIN Table1 T
    ON (T.srno - C.srno = 1)
      AND T.acctnum IS NULL
),
FINAL AS
(
  SELECT *, ROW_NUMBER() OVER(PARTITION BY acctnum ORDER BY Lvl DESC) RN
  FROM CTE
)
SELECT acctnum, newnote note
FROM FINAL
WHERE RN =1


SQL Server 2022 | db<>fiddle[^]

Result:
acctnum 	note
12 	 	 	abc efd xyz
15 	 	 	pqr
16 	 	 	uio njk uuuwr kmjkj
18 	 	 	zzzzzzz
 
Share this answer
 

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