0

My query is listing out comments in sequence lines. I am hoping to combine them into one comment column instead of having multiple rows for one PO. Here is my query now:

SELECT  PO,
    SEQUENCE_NUMBER,
    COMMENT_LINE
FROM Database

Which is returning:

PO SEQUENCE NUMBER COMMENT LINE
8582959 1 COMMENT LINE 1
8582959 2 COMMENT LINE 2
8582960 1 COMMENT LINE 1

I am hoping for it to return the two comment lines concatenated with a ', ' between them like this:

PO COMMENT LINE
8582959 COMMENT LINE 1, COMMENT LINE 2
8582960 COMMENT LINE 1

I tried using CONCAT, but couldn't figure out how to get the lines to join since they are in the same columns. I think maybe an inner join would work, but I have never used one and am not quite sure on how to format. Thanks so much for the help.

Jordan
  • 7
  • 3

1 Answers1

0

I don't know if it is available on Teradata but You can try using string_agg() aggregate function:

SELECT
  po,
  STRING_AGG(comment_line, ', ')
FROM your_table
GROUP BY po;
  • No STRING_AGG - but can be done leveraging XML_AGG https://stackoverflow.com/questions/45306260/concatenate-strings-from-multiple-records-in-teradata-sql/45306398#45306398 or NPATH https://stackoverflow.com/questions/63683088/teradata-results-with-duplicate-values-converted-into-comma-delimited-strings/63685218#63685218 functions (or even recursive query - but that's usually less performant). – Fred Oct 08 '21 at 17:01