This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output to count the repeated string and concat count with it.
Input data:
1 2 3 4 5 6 7 8 9 |
strvalue flag -------- ----------- ABC 0 ABC 1 XYZ 0 XYZ 1 PQR 0 PQR 1 PQR 1 |
Expected Output:
1 2 3 |
ConcatString -------------------- ABC*2 PQR*3 XYZ*2 |
Create a table with data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
CREATE TABLE tbl_strings ( strvalue varchar(5) ,flag int ) GO INSERT INTO tbl_strings VALUES ('ABC',0) ,('ABC',1) ,('XYZ',0) ,('XYZ',1) ,('PQR',0) ,('PQR',1) ,('PQR',1) GO |
Solution:
1 2 3 4 5 |
declare @res varchar(250) = '' select @res= @res + concat(strvalue,'*',SUM(flag) +1, ' ') from tbl_strings group by strvalue select @res AS ConcatString |
Please try the different solution for this puzzle and share it via comment...