Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>SQL is definitely not the right language to do string processing like that :-)</p> <p>I used existing code to split/create comma-delimited strings, but in TD14 it would be much easier (there's strtok_split_to_table and udfConcat).</p> <pre><code>CREATE VOLATILE TABLE vt (id INT, attrib VARCHAR(100)) ON COMMIT PRESERVE ROWS; INSERT INTO vt(1 ,'123(red), 139(red), 123(white), 123(black), 139(white),'); INSERT INTO vt(2 ,'123(black), 139(white), 123(green),'); INSERT INTO vt(32 ,'223(blue), 223(red), 553(white), 123(black),'); INSERT INTO vt(4 ,'323(white), 139(red), '); INSERT INTO vt(23 ,'523(red),'); WITH RECURSIVE cte (id, len, remaining, word, pos ) AS ( SELECT id, POSITION(',' IN attrib || ',') - 1 AS len, SUBSTRING(attrib || ',' FROM len + 2) AS remaining, TRIM(SUBSTRING(attrib FROM 1 FOR len)) AS word, 1 FROM vt UNION ALL SELECT id, POSITION(',' IN remaining)- 1 AS len_new, SUBSTRING(remaining FROM len_new + 2), TRIM(SUBSTRING(remaining FROM 1 FOR len_new)), pos + 1 FROM cte WHERE remaining &lt;&gt; '' ) SELECT id, MAX(CASE WHEN newpos = 1 THEN newgrp ELSE '' END) || MAX(CASE WHEN newpos = 2 THEN newgrp ELSE '' END) || MAX(CASE WHEN newpos = 3 THEN newgrp ELSE '' END) || MAX(CASE WHEN newpos = 4 THEN newgrp ELSE '' END) || MAX(CASE WHEN newpos = 5 THEN newgrp ELSE '' END) || MAX(CASE WHEN newpos = 6 THEN newgrp ELSE '' END) -- add as many CASEs as needed FROM ( SELECT id, ROW_NUMBER() OVER (PARTITION BY id ORDER BY newgrp) AS newpos, a || MAX(CASE WHEN pos = 1 THEN '(' || b ELSE '' END) || MAX(CASE WHEN pos = 2 THEN ', ' || b ELSE '' END) || MAX(CASE WHEN pos = 3 THEN ', ' || b ELSE '' END) || MAX(CASE WHEN pos = 4 THEN ', ' || b ELSE '' END) || MAX(CASE WHEN pos = 5 THEN ', ' || b ELSE '' END) || MAX(CASE WHEN pos = 6 THEN ', ' || b ELSE '' END) -- add as many CASEs as needed || '); ' AS newgrp FROM ( SELECT id, ROW_NUMBER() OVER (PARTITION BY id, a ORDER BY pos) AS pos, SUBSTRING(word FROM 1 FOR POSITION('(' IN word) - 1) AS a, TRIM(TRAILING ')' FROM SUBSTRING(word FROM POSITION('(' IN word) + 1)) AS b FROM cte WHERE word &lt;&gt; '' ) AS dt GROUP BY id, a ) AS dt GROUP BY id; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload