/ Published in: SQL
unroll variable length string list to normalised form.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
-- unrolls variable length list data to normalised form -- add another cross_join if number of elements in listdata >= 100.. SELECT t.id, substring_index(substring_index(t.list, ',', seq.n), ',', -1) element FROM variable_length_listdata t CROSS JOIN ( SELECT a1.n * 10 + a0.n + 1 n FROM digits_v a1 CROSS JOIN digits_v a0 ORDER BY n ) seq WHERE seq.n <= 1 + (LENGTH(t.list) - LENGTH(REPLACE(t.list, ',', ''))) ORDER BY t.id, element ;
URL: http://sqlfiddle.com/#!9/d1201/4