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

Sort conditional data in Postgres STRING_AGG

I've come up with a query to create concatenated string based on a conditional statement using STRING_AGG in Postgress. That works just fine but I want to sort the result as well without duplicating the CASE. This is what I have…
pean
  • 532
  • 1
  • 5
  • 13
1
vote
1 answer

DB ORACLE QUERY

I have a table where storing details ID NAME 1 A 2 A 1 A I need the output like ID Name Count 1,2 A 3 Please help to get the output like that in oracle select query
Sreejith A
  • 27
  • 7
1
vote
1 answer

Join two tables with a column with multiple entries for the other table

I have the following problem. I want to join two tables. The first table has entries like the following: T1 PK Info 1 one 2 two 3 three The second table is build like this: T2 PK FKT1 1 1,3 2 1,2,3 3 2 My Result…
Karl Le
  • 13
  • 2
1
vote
3 answers

SQL Server Convert Particular Column Values into comma separated String

I have a table in Database as below : Id Name 1 Item1 1 Item2 2 Item3 1 Item4 3 Item5 I need output as below(3rd column is count): 1 Item1,Item2,Item4 3 2 Item3 1 3 Item5 1 How it can achieved by SQL…
Mayank
  • 337
  • 2
  • 9
  • 22
1
vote
3 answers

Multiple value same id into single line in sql

Here is my table i want to add value in one line format my code is OrderDate OrderID ItemNo PaymentType Cash Nets Visa AMEX MasterCard Voucher Remarks ReciptNo 2018-01-27 809 40149 Mix Payment Type NULL NULL NULL …
Tushar
  • 182
  • 3
  • 19
1
vote
2 answers

SQL unique combinations

I have a table with three columns with an ID, a therapeutic class, and then a generic name. A therapeutic class can be mapped to multiple generic names. ID therapeutic_class generic_name 1 YG4 insulin 1 …
Molly
  • 49
  • 4
1
vote
1 answer

Aggregate string values to a list

I am trying to work some very simple logic to transform an unpivoted column into what essentially amounts to a grouped list. However, having troubles doing this efficiently. Essentially, I have a data set that looks as follows: CUST_ID ORDER 1 …
NickP
  • 1,354
  • 1
  • 21
  • 51
1
vote
3 answers

Comma separated list from multiple sql columns

Using SQL Server 2012, I have a table containing multiple checkbox fields where the values are 1 or 0, obviously. I need to pull any of these fields where any of these values are 1. So: ID Building Heavy Municipal …
RobF
  • 11
  • 2
1
vote
1 answer

postgresql string agg numeric/integer output result is wrong

As the title say, i need to concat the result query with string agg using this query (Without String Agg) select pdet.dept_id, pdet.grade_id from psa_aso_target ptar inner join psa_aso_targetdetails pdet on pdet.target_id=ptar.target_id and…
Alexander Chandra
  • 587
  • 2
  • 9
  • 23
1
vote
2 answers

Comma separated values with all rows from a table

I have a Services table with three columns. Service sr_id lang alias 1 EN A 1 PA B 1 HI C 2 EN D 2 HI E Now, I want to output with each service id, lang and alias column will be concatenation of alias for a…
Riya Bansal
  • 1,201
  • 1
  • 11
  • 10
1
vote
1 answer

How to get distinct prices by product with single row per product

Order contains same product with different prices. How to get list of distinct prices per product in order, with one row per product? I tried SELECT product, string_AGG(DISTINCT price::text, ',' ORDER BY price) FROM (VALUES ('A', 100), ('A', 200)…
Andrus
  • 26,339
  • 60
  • 204
  • 378
1
vote
1 answer

Group_Concat with join equivalent in SQL SERVER

I am having trouble in my code. I used to be on MySQL but I've migrated my codes into SQL Server. Now I am having trouble with this GROUP_CONCAT function. I found some 'potential' equivalent but it's just not resulting on what is expected. Here is…
WhiteMark
  • 39
  • 6
1
vote
1 answer

How to get multiple values in same cell in Oracle

I have a table in Oracle where there are two columns. In the first column, sometimes there are duplicate values that corresspond to a different value in the second column. How can I write a query that shows only unique values of the first column and…
siddhu
  • 369
  • 1
  • 6
  • 16
1
vote
1 answer

SQL String aggregation returns different results based on location of variables

My code: if object_id('tempdb..#t1') is not null drop table #t1 create table #t1 (ID int, name varchar(10)) insert into #t1 values (1,'2'), (6,'2'), (6,'2'), (1,'4') DECLARE @CHARS VARCHAR(100) = '' SELECT @CHARS = @CHARS + name + ', ' --…
Govind Rai
  • 14,406
  • 9
  • 72
  • 83
1
vote
1 answer

PostgreSQL - how to select elements from a cursor in a single row

I need to convert a query from Oracle SQL to Postgres. The query returns in a single row elements from a cursor. I'll write a simplified form of this query in Oracle: select CONCAT_LIST( cursor( select first_name || '-' || last_name from sys_users) …
Catalin Vladu
  • 389
  • 1
  • 6
  • 17