Questions tagged [string-agg]

Concatenates the values of string expressions and places separator values between them. The separator is not added at the end of string.

Applies to

SQL Server 2017 (14.x) and later YesAzure SQL Database YesAzure SQL Managed Instance yesAzure Synapse Analytics.

Syntax

STRING_AGG ( expression, separator ) [ <order_clause> ]

<order_clause> ::=   
    WITHIN GROUP ( ORDER BY <order_by_expression_list> [ ASC | DESC ] )   

Remarks

STRING_AGG is an aggregate function that takes all expressions from rows and concatenates them into a single string. Expression values are implicitly converted to string types and then concatenated. The implicit conversion to strings follows the existing rules for data type conversions. For more information about data type conversions, see CAST and CONVERT (Transact-SQL).

If the input expression is type VARCHAR, the separator cannot be type NVARCHAR.

Null values are ignored and the corresponding separator is not added. To return a place holder for null values, use the ISNULL function as demonstrated in example B.

STRING_AGG is available in any compatibility level.

Official Documentation

73 questions
0
votes
1 answer

Concatenate few rows into a single row with comma seperated using string_agg in postgreSQL

clique_bait.page_hierarchy CREATE TABLE clique_bait.page_hierarchy ( "page_id" INTEGER, "page_name" VARCHAR(14), "product_category" VARCHAR(9), "product_id" INTEGER ); sample input ('1', 'Home Page', null, null), ('2', 'All Products',…
Rajee
  • 21
  • 1
  • 5
0
votes
1 answer

Django: Advanced StringAgg with ManyToMany

Setup I have two tables: Person name Tim Tom Tam Pet | species | color | |---------|-------| | Cat | black | | Dog | brown | And a ManyToMany that connects…
MrTomRod
  • 345
  • 2
  • 16
0
votes
1 answer

Parse JSON file - Improve query in SQL Server

From this JSON File (just an example) I need to reach this final result { "Id": "101", "name": "C01", "testparameters": { "room": [ { "Floor": "First_Floor", "Rooms": ["Room1", "Room2",…
d2907
  • 798
  • 3
  • 15
  • 45
0
votes
1 answer

How to integrate SQL-Generated columns in Yii2 GridView

To show my GridView I use this ActiveDataProvider: public function search($params) { $query = PublicationsPublication::find() ->select(['eid', 'title', 'pubdate', 'citedby', "STRING_AGG(DISTINCT(CONCAT(author.authid, ' - ', authname)), ', ')…
Marco Cesana
  • 17
  • 1
  • 4
0
votes
1 answer

Query to Return N Random Concatenated Records

I was handed a task where I need to return a list of all the companies from one table and a comma-separated column of 3 random email addresses associated with that company from another table using Microsoft SQL Server. Thus far I have been able to…
crazybobcat
  • 19
  • 1
  • 5
0
votes
2 answers

PostgreSQL: Getting STRING_AGG to respect the ordering from a CTE

I'm creating a report, showing staff members' former teams along with the date they left, aggregated into a single field in the report. This works to a degree: WITH "most_recent_leave_dates" AS ( SELECT staff_id, -- alphanumeric string …
Jack Deeth
  • 3,062
  • 3
  • 24
  • 39
0
votes
1 answer

Find the most popular combinations SQL

I have 2 tables I want to join to explore the most popular combinations of location, by distinct id, ordered by count. I get location from l, date from d. The results from this join would be: id loc_id location date 1 111 NYC …
C_psy
  • 647
  • 8
  • 22
0
votes
1 answer

string_agg not behaving correctly

I have table like this : booking_id, state 01 red 01 green 01 black 02 red 02 green 03 red I wanted to get something like this : 01 red,green,black 02 …
0
votes
2 answers

More than one row returned by a subquery when using STRING_AGG function

I get an error when trying to execute SELECT query in PostgreSQL 11 select ( SELECT STRING_AGG(u.first_name::text, ', ') FROM game_authors AS gat LEFT JOIN users AS u ON u.id = gat.user_id WHERE gat.game_id = g.id AND gat.lang =…
0
votes
1 answer

Combination of Column Values in SQL Based on Common ID

Thanks for the help! I'm using MS SQL Server 17 and attempting to group by an ID and find common pairings in a second column based on the shared ID. Most other questions involved finding any combination between multiple columns. Here is some sample…
babbler
  • 3
  • 2
0
votes
3 answers

string_agg function in newer postgres version

I'm currently working on postgres code developed years ago under 8.4 version, and migrating to a 9.4 postgres version and I came across this line of code: string_agg(''#attribute_''||attribute_id::varchar||'':''||attribute) as attr Within this…
Matias
  • 539
  • 5
  • 28
0
votes
0 answers

SQL: Aggregation of string values from diffrent rows in a mulit-leveled JOIN

I have a multi-leveled table hierarchy in SQL Server, and when joining them I want to do an aggregation of strings from rows of one of the tables. In my (simplified) example in the screenshot below, I have Level1->Level2->Level3 and also a table…
user736570
  • 469
  • 4
  • 15
0
votes
1 answer

postgres I was given a set of number like "1,2,3,6,7,8,11,12,15,18,19,20" obtain maximum of each group consecutive numbers

the consecutive numbers are grouped by the query below, but I dont know how to obtain the maximum of each group of consecutive numbers with trans as ( select c1, case when lag(c1) over (order by c1) = c1 - 1 then 0 else 1 end as new …
gigi
  • 23
  • 5
0
votes
1 answer

How to show all products which not included in another table. by STRING_AGG

I am currently working on part of a project which is about products and variants. Product Table productID productName ------------------------- 1 Perfume X 2 Perfume Y Variants Table variantID variantName productID 1 …
Talal
  • 11
  • 2
0
votes
1 answer

T-SQL STRING_AGG problems dunno if bad writing or just not working

How come i don't see the seperation off my i.inventoryId in first part ? but when i select them in a other select i get way more results. also 'Jedi' == (PlayerAvatarId 6) https://i.ibb.co/XW4NJC7/image.png SELECT p.FirstName [Spelers…