Is there any equivalent of Postgres: STRING_AGG with OVER() or Oracle LISTAGG with OVER() in SQL Server? I would like to concatenate text values without grouping it, so I need STRING_AGG in analytical version, but according to docs is not supported in SQL Server:
The
OVERclause may follow all aggregate functions, except theSTRING_AGG,GROUPINGorGROUPING_IDfunctions.
Do I need to create subquery in my SELECT or is there better approach for that?
No,
STRING_AGGis only implemented as an aggregate function (at time of writing) with support for theWITHIN GROUPclause, it cannot be windowed (using anOVERclause). You will need to use a subquery/CTE/derived table:With no sample data, I can't test what would be most performant for you; you will need to do that off your own back (I expect the CTE and Derived table to have very similar, if not identical, query plans though).