0

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
coder rock
  • 113
  • 6

1 Answers1

2

Seems like a pretty straight-forward query with STRING_AGG and GROUP BY:

SELECT 
    h.HospitalId, h.Name,
    STRING_AGG(d.DivisionName, ',') AS DivisonName
FROM 
    dbo.Division d
INNER JOIN
    dbo.Hospital h ON h.HospitalId = d.HospitalId
GROUP BY
    h.HospitalId, h.Name

UPDATE

For versions before SQL Server 2017, you need to use the FOR XML PATH approach - something like this:

SELECT 
    h.HospitalId, h.Name,
    STUFF ((SELECT ',' + d.DivisionName
            FROM @Division d
            WHERE d.HospitalId = h.HospitalId
            FOR XML PATH('')), 1, 1, '') 
FROM 
    @Hospital h 
GROUP BY
    h.HospitalId, h.Name
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • i dont want using STRING_AGG is not working in latest sql version. @marc_s – coder rock Jan 02 '23 at 09:01
  • STRING_AGG is working on older sql server versions and not working on new version sql servers. @marc_s – coder rock Jan 02 '23 at 09:06
  • 1
    @mohdmazharkhan: `STRING_AGG` was introduced in SQL Server **2017**; and it works just fine in 2017, 2019 and 2022 versions of SQL Server. Why not use it, when it's available?? ,, – marc_s Jan 02 '23 at 09:08
  • my question query is working in older version but prodcution this query is not working 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 . @marc_s – coder rock Jan 02 '23 at 09:10
  • and what is this link is saying: stackoverflow.com/a/48804363/6467840 .@marc_s – coder rock Jan 02 '23 at 09:11
  • @mohdmazharkhan: what version is your production server then?? `STRING_AGG` works in SQL Server **2017 and newer** but ***NOT*** in older versions than 2017 .... – marc_s Jan 02 '23 at 09:11
  • sorry i have 2016 server version in production. @marc_s – coder rock Jan 02 '23 at 09:15
  • @mohdmazharkhan: updated with an approach that'll work in older SQL Server versions, without `STRING_AGG` – marc_s Jan 02 '23 at 09:21
  • thanks i will check and update your answer. – coder rock Jan 02 '23 at 09:22