How to GROUP_CONCAT in Ingres?

僤鯓⒐⒋嵵緔 提交于 2020-01-15 16:47:26

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16795370/how-to-group-concat-in-ingres

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!