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
2 answers

Restrict string_agg order by in postgres

While working with postgres db, I came across a situation where I will have to display column names based on their ids stored in a table with comma separated. Here is a sample: table1 name: labelprint id field_id 1 1,2 table2 name: datafields id…
1
vote
1 answer

SQL Server: select from table with/without aggregate columns using groupby

I have the columns and rows for the table A, tableid featureid col3 col4 coldatetime 1 1 AD 4 2022-06-22 09:00:00 2 2 BC 5 2022-06-22 09:00:00 3 1 AE 6 2022-06-22 10:00:00 4 3 BD 7 2022-06-22 11:00:00 5 2 BB 8 2022-06-22…
1
vote
0 answers

PostreSQL, string_agg() join through association table

I have 2 tables linked together via 3rd association table: TABLE NAME: lot id | description | | 1 | descr_string_1 | ... | 2 | descr_string_2 | ... | TABLE NAME: id_class id…
Krank
  • 141
  • 1
  • 8
1
vote
1 answer

Use String_AGG to query with condition in SQL?

I have 3 tables with relationship is Policy (N) -> PolicyService <- Service (N): DXBusinessPolicy_Policy ID Code Name 1 COMBO.2103001 [Giá nền] T9/2020 #1 2 IPTV-0121.002 [Giá nền] T8/2020 #1 3 INT.2103001 Chính sách…
1
vote
2 answers

Create new column with all unique values from another column in SQL

I have the following table: >>> id crop grower loc 0 11 maize Lulu Fiksi 1 13 maize Lulu Menter 2 05 maize Felix Hausbauch 3 04 apples Lulu Fiksi 4 02 apples Meni Linter 5 06 cotton …
Reut
  • 1,555
  • 4
  • 23
  • 55
1
vote
2 answers

SQL Rows concatenation based on single column

I have Table with values X Y Z - --- - 1 abc P 1 def Q I need a normal query (not pl/sql) which can give result as X Y Z - ------- --- 1 abc,def P,Q i.e Based on column X the values are converted into csv…
C Deepak
  • 1,258
  • 1
  • 17
  • 23
1
vote
2 answers

Group products into one cell - Mysql

I've this table Orders : +-----+--------------+ | id_order | products| +----------+---------+ | 1 | product 1 | +-----+--------------+ | 1 | product 49| +-----+--------------+ | 1 | product 12| +-----+--------------+ | 2 | …
1
vote
1 answer

SQL Combine duplicate rows while concatenating one column

I have a table (example) of orders show below. The orders are coming in with multiple rows that are duplicated for all columns except for the product name. We want to combine the product name into a comma delimited string with double quotes. I would…
Nick Kester
  • 83
  • 1
  • 12
1
vote
1 answer

How to populate a PostgresQL column with a list of all values from column1 from all rows with the current row’s value of column2

I have a Postgres table with the following format: Fruit Owner Apple John Orange Susan Pear Michael Peach Susan I want to write a query that creates a new column, Owner's Fruits, which lists all fruits owned by the owner of the row…
223seneca
  • 1,136
  • 3
  • 19
  • 47
1
vote
2 answers

Substitute for STRING_AGG pre SQL Server 2016

I need to group a table by a set of values together with all matching row numbers/id:s for each set. This operation must be done within the boundaries of SQL Server 2016. Let's suppose I have the following table…
Hwende
  • 599
  • 2
  • 10
  • 25
1
vote
2 answers

Concatenate SQL columns with comma separated

Is there any alternate way to concatenate SQL columns with comma separated. I am using below logic for concatenation. The columns (col1,col2,col3) can have null values. select stuff( left(concat(col1,',',col2,',',col3), …
MRR
  • 83
  • 3
  • 9
1
vote
0 answers

'GROUP_CONCAT' is not a recognized built-in function name. - Incorrect syntax near the keyword 'FOR'

I am using SQL Server Management Studio v15.0 and I'm trying to use GROUP_CONCAT() or STUFF() functions to get the concatenated result. But I am unable to draft a correct query. I am getting the errors mentioned in the tagline. I have 2 tables, User…
CCA
  • 11
  • 1
  • 3
1
vote
3 answers

How to get strings comma separated in ascending order with STRING_AGG()

My table looks like this Color Order ------------ Red 49 Blue 32 Green 80 Green 30 Blue 93 Blue 77 Red 20 Green 54 Red 59 Red 42 Red 81 Green 35 Blue 91 My Query is SELECT Color, Count(*) AS Count, STRING_AGG(Order,',')…
asmgx
  • 7,328
  • 15
  • 82
  • 143
1
vote
1 answer

SQL server query to search and stuff multiple rows

I have table employee_table which is like this org employeeid (int) firstname lastname 1234 56788934 Suresh Raina 1234 56793904 Virat Kohli then i have project_table which is like this Project …
curiousboy
  • 135
  • 2
  • 13
1
vote
1 answer

string_agg(character_varying) doesnt exist

I am doing a migration from MSSQL to PostgreSQL MSSQL query: SELECT * FROM dbo.Mtr_userdetails AS table1 JOIN( SELECT urmo.urm_userid STUFF((SELECT ',' + urm.urm_role_name FROM dbo.STL_CS_Mtr_userrolemapping urm WHERE urm.urm_userid = 'test2' AND…
Rlaks
  • 7
  • 1
  • 5