ListAGG in SQLSERVER

后端 未结 4 1271
心在旅途
心在旅途 2020-11-21 22:22

I\'m trying to aggregate a \'STRING\' field in SQLServer. I would like to find the same function LISTAGG like in Oracle .

Do you know how to do the same function or

4条回答
  •  礼貌的吻别
    2020-11-21 23:25

    This might be useful to someone also ..

    i.e. For a data analyst and data profiling type of purposes ..(i.e. not grouped by) ..

    Prior to the SQL*Server 2017 String_agg function existence ..

    (i.e. returns just one row ..)

    select distinct
    SUBSTRING (
    stuff(( select distinct ',' + [FieldB] from tablename order by 1 FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') 
    ,1,0,'' ) 
    ,2,9999)
    from 
        tablename 
    

    e.g. returns comma separated values A,B

提交回复
热议问题