37

If I have data like this in a table

id   data
--   ----
1    1
1    2
1    3
2    4
2    5
3    6
3    4

How do I get results like this in a query (on sybase server)?

id   data
--   ----
1    1, 2, 3
2    4, 5
3    6, 4
Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
Jagmal
  • 5,078
  • 9
  • 33
  • 34
  • 1
    For HiveQL, see https://stackoverflow.com/questions/16444070/how-to-get-array-bag-of-elements-from-hive-group-by-operator. – flow2k Jul 01 '19 at 23:12

12 Answers12

13

I know that in MySQL there is GROUP_CONCAT and in Sybase I think it's LIST as stated in another answer:

SELECT id, LIST(data||', ')
FROM yourtable
GROUP BY id
lpfavreau
  • 11,693
  • 5
  • 28
  • 35
7

For PostgreSQL, using a similar function string_agg.

SELECT id, string_agg(data, ',')
FROM yourtable
GROUP BY id
Ringtail
  • 231
  • 3
  • 3
5

In MsSQL you can use a function (Don't know if there is somenthing similar in SyBase)

CREATE FUNCTION [dbo].[GetDataForID]
(
    @ID int
)
RETURNS varchar(max)
AS
BEGIN
    declare @output varchar(max)
    select @output = COALESCE(@output + ', ', '') + data
    from table
    where ID = @ID

    return @output
END
GO

And then:

SELECT ID, dbo.GetDataForID(ID) as Data
FROM Table
GROUP BY ID
Eduardo Molteni
  • 37,007
  • 23
  • 135
  • 201
4

In PL/SQL you can do it by:

SELECT id, LISTAGG(data, ',') WITHIN GROUP(ORDER BY 0) "data"
  FROM yourtable
 GROUP BY id
Yan Pak
  • 1,299
  • 1
  • 14
  • 11
4

In mysql, use

SELECT id, GROUP_CONCAT(data)
 FROM yourtable
 GROUP BY id

or use your custom separator:

SELECT id, GROUP_CONCAT(data SEPARATOR ', ')
 FROM yourtable
 GROUP BY id

see GROUP_CONCAT.

puppylpg
  • 325
  • 2
  • 10
  • link on GROUP_CONCAT has moved: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat – ThePhi Apr 02 '21 at 06:34
3

You can't do this in a straight GROUP BY in plain-vanilla SQL. You have to use a cursor (or a similar construct) to concatenate the values in each group manually.

  • Oracle lets you define a custom aggregator which would do this concatenation in PL/SQL.
  • SQL Server lets you define a custom aggregator in .NET which would do this, as well.
  • I'm not sure about what options Sybase has for defining custom aggregators, though.
Dave Markle
  • 88,065
  • 20
  • 140
  • 165
1

Try this one:

SELECT id,
       GROUP_CONCAT(data)
FROM   table
GROUP BY id
TLama
  • 71,521
  • 15
  • 192
  • 348
Aziz
  • 21
  • 1
1

I think you're going to have to use a cursor (http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@Generic__BookTextView/50501;pt=50305)

Dan Williams
  • 4,732
  • 9
  • 34
  • 46
1

In SQL Server:

select distinct b.id, data=STUFF((select ',' +convert(varchar,id)
from yourtable a
where a.id=b.id
for xml path ('')),1,1,'')
from yourtable b
Nishu Tayal
  • 18,079
  • 8
  • 44
  • 90
eitan
  • 11
  • 1
1

For Presto, use array_agg

SELECT id, array_agg(data)
FROM yourtable
GROUP BY id
TheLioness
  • 11
  • 1
0

It's been a few years since I've tried using the syntax and I no longer have access to an iAnywhere instance, but there was an aggregate function (list) to do such a task. I cannot confirm if LIST() is still supported.

SELECT id,
       LIST(data)
FROM   table
GROUP BY id
KDrewiske
  • 1,501
  • 1
  • 16
  • 17
  • Doesn't look like it is supported in SQL Server 2008 R2 (10.50.2500) ("'LIST' is not a recognized built-in function name.") – John Bubriski Feb 11 '14 at 16:41
0

For SQL server:

SELECT id, STRING_AGG(data, ',')
FROM your_table
GROUP BY id;