1

Attached Image

I have the sample values as shown in the image attached. What I want to achieve is that the value of PR_NUMBER field gets concatenated on the basis of same values in PO_NUMBER and PO_ITEM.

Though this is a sample data, any n number of rows can have the same values and hence the concatenation of all such values in the PR_NUMBER column need to be done.

I got to know about CURSORS to loop through the table but don't really know what to do with them.

Expected output image is

123 | 1 | 5678,6789
456 | 1 | 2322,3432
456 | 2 | 4678

2 Answers2

0

You are looking for group_concat():

select po_number, po_item, group_concat(pr_number)
from t
group by po_number, po_item;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

If you are using SAP HANA then you can use STRING_AGG(Name, ',')

 SELECT po_number, po_item, STRING_AGG(pr_number, ',')
 from your_table
 group by po_number, po_item;

instead if you are using mysql you can use group_concat

 SELECT po_number, po_item, group_concat(pr_number, ',')
 from your_table
 group by po_number, po_item;
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Thank you! I figured out already that STRING_AGG is the substitute to GROUP_CONTACT. By the way, is there any way to do it without the function? – Rahul Lamba Sep 09 '17 at 14:04
  • whit sql not .. if you don't want use a db brand related function you must performe the aggregation using a procedural approach on a full select .. – ScaisEdge Sep 09 '17 at 16:02