concatenate arbitrary long list of matches in SQL subquery

Posted by lordvlad on Stack Overflow See other posts from Stack Overflow or by lordvlad
Published on 2013-10-15T13:24:46Z Indexed on 2013/10/18 15:55 UTC
Read the original article Hit count: 182

Filed under:
|

imagine 2 tables (rather stupid example, but for the sake of simplicity, here you go)

words
  word_id

letters
  letter
  word_id

how can i select all words while selecting all letters that belong to a word and concatenating them to said word? it is important that the letters are returned in the order they appear in the table, as the letter may be mixed into other words, but the order is correct.

|word_id|    |word_id|letter|
+-------+    +-------+------+
|      1|    |      1|     H|
|      2|    |      2|     B|
             |      2|     Y|
             |      1|     I|
             |      2|     E|

should return

|word_id|word|
+-------+----+
|      1|  HI|
|      2| BYE|

any way to accomplish this in pure SQL?

© Stack Overflow or respective owner

Related posts about sql

Related posts about firebird