Questions tagged [string-aggregation]

This tag is intended for SQL questions where multiple string (character) values should be aggregated into a single value using GROUP BY. The equivalent of sum() - just for strings.

The ISO/IEC SQL standard function for this is LISTAGG, but several DBMS:es have their own string aggregation functions:

386 questions
1
vote
1 answer

Limit Number of elements in a string aggregation query

I have below table and string aggregation using XML: CREATE TABLE TestTable ([ID] INT, [Name] CHAR(1)) INSERT INTO TestTable ([ID],[Name]) VALUES (1,'A') INSERT INTO TestTable ([ID],[Name]) VALUES (2,'B') …
1
vote
2 answers

SQL Rows with intended duplicate IDs - How to join them up?

I've got some user and department tables that are joined together, but with the option on the front end to include multiple "Leader" email addresses. Ideally, I'd like to keep these separated out on the main dataset; however, on the reporting view,…
1
vote
1 answer

Concatenate fields using connect by prior

Having the following table my_tabe: M01 | 1 M01 | 2 M02 | 1 I want to query over it in order to obtain: M01 | 1,2 M02 | 1 I managed to get close using the following query: with my_tabe as ( select 'M01' as scycle, '1' as sdate from…
filippo
  • 5,583
  • 13
  • 50
  • 72
1
vote
2 answers

One-to-Many SQL SELECT concatenated into single row

I'm using Postgres and I have the following schemes. Orders | id | status | |----|-------------| | 1 | delivered | | 2 | recollected | Comments | id | text | user | order | |----|---------|------|-------| | 1 | texto 1 | 10 | 20 …
Hugo Licon
  • 191
  • 2
  • 12
1
vote
1 answer

Using Stuff to roll up data from multiple rows AND concatenate columns

I've found similar questions on the site, but I'm still struggling with this. I have a table with information like the below: AcctNo ChargeOrder ChargeCode ABC 1 Charge1 ABC 2 Charge2 ABC 3 …
1
vote
2 answers

Concatenate rows into columns (NO FOR XML PATH('') and recursive CTEs) - SQL Server 2012

I have a very particular problem at hand. Brief introduction: I have two columns at a database that I need to "group concatenate", in MySQL I would simply use GROUP_CONCAT to get the desired result, in SQL Server 2017 and on I would use STRING_AGG,…
1
vote
1 answer

I can't figure out how to Order by with string_agg

I have this query (I am using SQL Server 2019) and is working fine (combining Dates and Notes into one column). However, the result I am looking for is to have the latest date show up first. How can I achieve that from this query? SELECT ID, …
peka
  • 51
  • 1
  • 7
1
vote
1 answer

Convert Multiple Rows into One - SQL

I'm trying to have the 'test' column in the same row. I'm using Stuff() however, it seems that the 'test' column is going through all the qID What am I missing? SELECT DISTINCT qID, STUFF(( select ',' + B.text …
Camus
  • 827
  • 2
  • 20
  • 36
1
vote
2 answers

SQL Grouping of Columns Values

I would like to GROUP cols into 1 record using a distinctive name id, code, date, name -------------- 1, code1, date1, name1 2, code2, date2, name1 3, code3, date3, name2 I would like the result to be record1: "code1,date1,code2,date2",…
Shazam
  • 105
  • 1
  • 7
1
vote
1 answer

TSQL Array Initialization and LINQ Where on Array

I'm trying to perform string aggregation on an Address in T-SQL so that any of the Address's fields which are not NULL are used in a comma-separated string. I know how I would do this in C# but am lost with SQL. Here's what I have so far (I need to…
Matt Arnold
  • 668
  • 2
  • 8
  • 21
1
vote
3 answers

Aggregate without duplicates

What I have going on here is: Table 1: AOC_Model AOC_ID int (Primary Key) Model varchar(50) Table 2: AOC_Chipset AOC_CHIPSET_ID int (Primary Key) CONTROLLER_ID int CHIPSET_ID int AOC_ID int Table 3: Controller CONTROLLER_ID int (Primary…
peka
  • 51
  • 1
  • 7
1
vote
3 answers

Select multiple data from one column and update the combine result to another table

I have a table 'table A' as below and I want to select these data into one column and the result will update table B +-----------+ | ID | Name | +-----------+ | 1 |a | +-----------+ | 2 |b | +-----------+ | 3 |c …
Fate
  • 37
  • 6
1
vote
1 answer

Redshift SQL to comma separate a field with GROUP

I want to comma separate a fields values and GROUP BY two other fields in Redshift. Sample data: table_schema table_name column_name G1 G2 a G1 G2 b G1 G2 c G1 G2 d G3 …
Matt
  • 14,906
  • 27
  • 99
  • 149
1
vote
1 answer

Sql query , grouping result from left outer join to a single cell/field of main table

Let's say that i have the following 3 tables : IDUser Name Surname 1 Lucas Wurth 2 John Charson 3 Erik Drown IDUser IDLocation 1 1 1 2 2 1 …
3Daygg
  • 63
  • 1
  • 7
1
vote
2 answers

How do I concatenate fragmented messages of strings that are out of order in SQL

I have a table with three columns the first column indicates a message ID (message_id) the second column represents an ordinal feature which indicates the order of the message (message_order), lastly the third column is a fragment of the…
Jose
  • 31
  • 5