I have below SQL query which is not working on SQL Server 2016 and below version
SELECT
(
SELECT STRING_AGG(d.Name, ',') AS divnames
FROM (
SELECT div.name, MHLId
FROM MsrtProfile mp
INNER JOIN Division div ON mp.DivisionId = div.Id
WHERE mp.mhlid = ph.potentialHospitalNo
GROUP BY div.Name, MHLId
) d
) AS divisionnames
FROM xyz ph
Example my output will come like below
div1,div2,div3
I need with comma values of single column rows.
I have two tables are following below - first table name is Hospital
:
hospitalId name
-----------------
1 a1
2 a2
Second table name is division
id DivisionName hospitalId
------------------------------
1 d1 1
2 d2 1
3 d3 2
I need an output like below by join first and second table by hospitalid
DivisionName
-------------
d1,d2
d3