Day 358 Problem 2 Solved
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Code\n```mysql []\n# Write your MySQL query statement below\n\nWITH recursive content_sep_words AS (\n    SELECT\n        content_id,\n        content_text,\n        \' \' as delim,\n        case when instr(content_text, \' \') = 0 and instr(content_text, \'-\') = 0 then content_text\n        when instr(content_text, \' \') <> 0 and instr(content_text, \'-\') <> 0 and instr(content_text, \' \') < instr(content_text, \'-\') then substring(content_text, 1, INSTR(content_text, \' \') - 1)\n        when instr(content_text, \' \') <> 0 and instr(content_text, \'-\') <> 0 and instr(content_text, \'-\') < instr(content_text, \' \') then substring(content_text, 1, INSTR(content_text, \'-\') - 1)\n        when instr(content_text, \' \') <> 0 and instr(content_text, \'-\') = 0 then substring(content_text, 1, INSTR(content_text, \' \') - 1)\n        when instr(content_text, \' \') = 0 and instr(content_text, \'-\') <> 0 then substring(content_text, 1, INSTR(content_text, \'-\') - 1)\n        end AS word,\n        case when instr(content_text, \' \') = 0 and instr(content_text, \'-\') = 0 then \'\'\n        when instr(content_text, \' \') <> 0 and instr(content_text, \'-\') <> 0 and instr(content_text, \' \') < instr(content_text, \'-\') then substring(content_text, INSTR(content_text, \' \'), 1000)\n        when instr(content_text, \' \') <> 0 and instr(content_text, \'-\') <> 0 and instr(content_text, \'-\') < instr(content_text, \' \') then substring(content_text, INSTR(content_text, \'-\'), 1000)\n        when instr(content_text, \' \') <> 0 and instr(content_text, \'-\') = 0 then substring(content_text, INSTR(content_text, \' \'), 1000)\n        when instr(content_text, \' \') = 0 and instr(content_text, \'-\') <> 0 then substring(content_text, INSTR(content_text, \'-\'), 1000)\n        end AS remaining,\n        1 AS word_level\n        from user_content\n    UNION ALL\n    SELECT \n        content_sep_words.content_id,\n        content_sep_words.content_text,\n        substr(remaining, 1, 1) as delim,\n        case when instr(substring(remaining, 2, 1000), \' \') = 0 and instr(substring(remaining, 2, 1000), \'-\') = 0 then substring(remaining, 2, 1000)\n        when instr(substring(remaining, 2, 1000), \' \') <> 0 and instr(substring(remaining, 2, 1000), \'-\') <> 0 and instr(substring(remaining, 2, 1000), \' \') < instr(substring(remaining, 2, 1000), \'-\') then substring(substring(remaining, 2, 1000), 1, INSTR(substring(remaining, 2, 1000), \' \') - 1)\n        when instr(substring(remaining, 2, 1000), \' \') <> 0 and instr(substring(remaining, 2, 1000), \'-\') <> 0 and instr(substring(remaining, 2, 1000), \'-\') < instr(substring(remaining, 2, 1000), \' \') then substring(substring(remaining, 2, 1000), 1, INSTR(substring(remaining, 2, 1000), \'-\') - 1)\n        when instr(substring(remaining, 2, 1000), \' \') <> 0 and instr(substring(remaining, 2, 1000), \'-\') = 0 then substring(substring(remaining, 2, 1000), 1, INSTR(substring(remaining, 2, 1000), \' \') - 1)\n        when instr(substring(remaining, 2, 1000), \' \') = 0 and instr(substring(remaining, 2, 1000), \'-\') <> 0 then substring(substring(remaining, 2, 1000), 1, INSTR(substring(remaining, 2, 1000), \'-\') - 1)\n        end AS word,\n        case when instr(substring(remaining, 2, 1000), \' \') = 0 and instr(substring(remaining, 2, 1000), \'-\') = 0 then \'\'\n        when instr(substring(remaining, 2, 1000), \' \') <> 0 and instr(substring(remaining, 2, 1000), \'-\') <> 0 and instr(substring(remaining, 2, 1000), \' \') < instr(substring(remaining, 2, 1000), \'-\') then substring(substring(remaining, 2, 1000), INSTR(substring(remaining, 2, 1000), \' \'), 1000)\n        when instr(substring(remaining, 2, 1000), \' \') <> 0 and instr(substring(remaining, 2, 1000), \'-\') <> 0 and instr(substring(remaining, 2, 1000), \'-\') < instr(substring(remaining, 2, 1000), \' \') then substring(substring(remaining, 2, 1000), INSTR(substring(remaining, 2, 1000), \'-\'), 1000)\n        when instr(substring(remaining, 2, 1000), \' \') <> 0 and instr(substring(remaining, 2, 1000), \'-\') = 0 then substring(substring(remaining, 2, 1000), INSTR(substring(remaining, 2, 1000), \' \'), 1000)\n        when instr(substring(remaining, 2, 1000), \' \') = 0 and instr(substring(remaining, 2, 1000), \'-\') <> 0 then substring(substring(remaining, 2, 1000), INSTR(substring(remaining, 2, 1000), \'-\'), 1000)\n        end AS remaining,\n        word_level + 1\n    FROM content_sep_words\n    WHERE remaining <> \'\'\n),\ncontent_sep_words_capitalized as (\n    select content_id,\n    content_text,\n    word_level as word_seq,\n    word,\n    CONCAT(coalesce(delim, \'\'), upper(substring(WORD, 1, 1)), lower(substring(WORD, 2, 1000))) as formatted_word\n    from content_sep_words\n)\nselect\ncontent_id,\ncontent_text as original_text,\nsubstr(group_concat(formatted_word order by word_seq separator \'\'), 2, 1000) as converted_text\nfrom content_sep_words_capitalized\ngroup by 1, 2\norder by 1\n```
['MySQL']