Hi @ahmed salah ,
Please try the following solution.
It seems there is no need to use XQuery FLWOR expression.
SQL
-- DDL and sample data population, start
DECLARE @FinalCountrisData table
(
CompanyId int,
StrtYear int,
StrtRevisionId int,
countriesValues int
);
INSERT INTO @FinalCountrisData (CompanyId,StrtYear,StrtRevisionId,countriesValues) VALUES
(121,2010,12331,888),
(121,2010,12331,999),
(121,2010,12331,666),
(121,2011,12331,111),
(121,2011,12331,333),
(121,2011,12331,555),
(121,2012,12331,222),
(121,2012,12331,444),
(121,2012,12331,777);
-- DDL and sample data population, end
SELECT CompanyId,
StrtYear,
StrtRevisionId,
STUFF((
SELECT '|' + TRY_CAST(countriesValues AS VARCHAR(10)) AS [text()]
FROM @FinalCountrisData AS c
WHERE c.CompanyId = p.CompanyId
AND c.StrtYear = p.StrtYear
AND c.StrtRevisionId = p.StrtRevisionId
ORDER BY countriesValues
FOR XML PATH('')
),1,1,'') AS CountriesDataStuff
FROM @FinalCountrisData AS p
GROUP BY CompanyId, StrtYear, StrtRevisionId;
Output
+-----------+----------+----------------+--------------------+
| CompanyId | StrtYear | StrtRevisionId | CountriesDataStuff |
+-----------+----------+----------------+--------------------+
| 121 | 2010 | 12331 | 666|888|999 |
| 121 | 2011 | 12331 | 111|333|555 |
| 121 | 2012 | 12331 | 222|444|777 |
+-----------+----------+----------------+--------------------+