Search code examples
sqlingres

How to GROUP_CONCAT in Ingres?


Is there any easy way you can simulate GROUP_CONCAT functionality in Ingres 9.2?

I have a table which has something like :

OrderID   LineNumber    LineText
1         1             This is an example note which is trunc
1         2             ated at a certain point.
2         1             Another note which is just one line.

And so on. Some notes are 1 line, others are 50+ lines.

I want a query to return:

OrderID  FullText
1        This is an example note which truncated at a certain point.
2        Another note which is just one line.

In MySQL or SQLite I'd use GROUP_CONCAT. In MS SQL it's more difficult but I'd use the FOR XML functionality to achieve a solution. I'm not sure how I could do this in Ingres. I started writing a stored procedure which could return the concatenated notes for a single order id, but I couldn't see an easy way of integrating that into my queries.

Any ideas?


Solution

  • This might work:

    select OrderId,
           (max(case when LineNumber = 1 then LineText else '' end) +
            max(case when LineNumber = 2 then LineText else '' end) +
            max(case when LineNumber = 3 then LineText else '' end)
           ) as LineText
    from t
    group by Orderid;
    

    It is very convenient that you have LineNumber.