without GROUP_CONCT and similar database functions)

Do you mean without a special group concatenation function(), or without any functions at all? This can be easily done with a recursive query.

WITH Builder(line_n, word, word_position, amount) AS ( SELECT line_n, '', 0, MAX(word_position) FROM table GROUP BY line_n UNION ALL SELECT Data.line_n, CONCAT(Builder.word, CONCAT(' ', Data.word)), Data.word_position, Builder.amount FROM Builder, table WHERE table.line_n = table.line_n AND table.word_position = table.word_position + 1 ) SELECT line_n, word FROM Builder WHERE word_position = amount;

The double CONCAT() was used to avoid casting issues; you may be able to remove them. Depending on your RDBMS, you may need to specify the RECURSIVE keyword.

Also, if you don't mind, a side point, please do not use * outside of COUNT(), EXISTS(), and ad hoc queries. Specifying the columns--id, line_n, word, word_position--is easy, self-documenting, and protects against column changes and reordering.

Perhaps it is worth mentioning, the id column is redundant, as line_n and word_position must be unique anyway.


In reply to Re: Fetching data from DB and complex data structures by chacham
in thread Fetching data from DB and complex data structures by frasco

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.