0

Edited: In SQL, I’m joining a table with a text column to a separate table (the latter not depicted in my example below). I would like to display the text from each row concatenated as a single column in a row. Using a separate column (Line), I’d like to use to determine the order in which the text is concatenated.

Col   Line   Text
a     1      This
a     4      sentence
a     3      a
a     2      is

Output in a single column/row:

This is a sentence
4
  • 1
    Are you asking about SQL or some other language? Is it a database or in-memory table of some sort? Commented Nov 27, 2019 at 16:15
  • You can use ORDER BY Line to display the rows in the desired order, but it will take programming to concatenate the values of Str into a string. Commented Nov 27, 2019 at 16:19
  • Hi, it will be possible to help if you provide a language you are using and any extra details (e.g. things you have tried) Commented Nov 27, 2019 at 16:29
  • 1
    string aggregation differs depending on the DBMS. For example MySql - GROUP_CONCAT, Oracle DB - LISTAGG, Sql Server - STRING_AGG, PostgreSql - STRING_AGG. So knowing your DBMS & version might be helpfull. Commented Nov 27, 2019 at 17:15

2 Answers 2

1

In MySql try:

select  group_concat(Str order by Line separator ' ' ) as Sentence FROM Table1
group by Col;

Result:

sentence
This is a sentence

http://sqlfiddle.com/#!9/e84f9e/5

Sign up to request clarification or add additional context in comments.

1 Comment

Rofl, 2 answers for the price of one. Nice one.;)
1

Try this in postgresql:

select  string_agg(Str,' ' order by Line ) as Sentence FROM Table1
group by Col;

Output:

sentence
This is a sentence

http://sqlfiddle.com/#!17/e84f9/4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.