-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathfirst-letter-capitalization-ii.sql
74 lines (72 loc) · 3.96 KB
/
first-letter-capitalization-ii.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Time: O(n * l^2 + (n * l) * log(n * l))
# Space: O(n * l^2)
# recursive cte
WITH RECURSIVE splitted_word_cte AS (
SELECT content_id,
content_text,
'^' AS delim,
CASE
WHEN INSTR(content_text, ' ') = 0 AND INSTR(content_text, '-') = 0 THEN
content_text
WHEN INSTR(content_text, ' ') != 0 AND INSTR(content_text, '-') = 0 THEN
SUBSTRING(content_text, 1, INSTR(content_text, ' ') - 1)
WHEN INSTR(content_text, ' ') = 0 AND INSTR(content_text, '-') != 0 THEN
SUBSTRING(content_text, 1, INSTR(content_text, '-') - 1)
WHEN INSTR(content_text, ' ') != 0 AND INSTR(content_text, '-') != 0 THEN
SUBSTRING(content_text, 1, INSTR(content_text, IF(INSTR(content_text, ' ') < INSTR(content_text, '-'), ' ', '-')) - 1)
END AS word,
CASE
WHEN INSTR(content_text, ' ') = 0 AND INSTR(content_text, '-') = 0 THEN
''
WHEN INSTR(content_text, ' ') != 0 AND INSTR(content_text, '-') = 0 THEN
SUBSTRING(content_text, INSTR(content_text, ' '), 255)
WHEN INSTR(content_text, ' ') = 0 AND INSTR(content_text, '-') != 0 THEN
SUBSTRING(content_text, INSTR(content_text, '-'), 255)
WHEN INSTR(content_text, ' ') != 0 AND INSTR(content_text, '-') != 0 THEN
SUBSTRING(content_text, INSTR(content_text, IF(INSTR(content_text, ' ') < INSTR(content_text, '-'), ' ', '-')), 255)
END AS remain,
1 AS idx
FROM user_content
UNION ALL
SELECT
content_id,
content_text,
SUBSTR(remain, 1, 1) AS delim,
CASE
WHEN INSTR(SUBSTRING(remain, 2, 255), ' ') = 0 AND INSTR(SUBSTRING(remain, 2, 255), '-') = 0 THEN
SUBSTRING(remain, 2, 255)
WHEN INSTR(SUBSTRING(remain, 2, 255), ' ') != 0 AND INSTR(SUBSTRING(remain, 2, 255), '-') = 0 THEN
SUBSTRING(SUBSTRING(remain, 2, 255), 1, INSTR(SUBSTRING(remain, 2, 255), ' ') - 1)
WHEN INSTR(SUBSTRING(remain, 2, 255), ' ') = 0 AND INSTR(SUBSTRING(remain, 2, 255), '-') != 0 THEN
SUBSTRING(SUBSTRING(remain, 2, 255), 1, INSTR(SUBSTRING(remain, 2, 255), '-') - 1)
WHEN INSTR(SUBSTRING(remain, 2, 255), ' ') != 0 AND INSTR(SUBSTRING(remain, 2, 255), '-') != 0 THEN
SUBSTRING(SUBSTRING(remain, 2, 255), 1, INSTR(SUBSTRING(remain, 2, 255), IF(INSTR(SUBSTRING(remain, 2, 255), ' ') < INSTR(SUBSTRING(remain, 2, 255), '-'), ' ', '-')) - 1)
END AS word,
CASE
WHEN INSTR(SUBSTRING(remain, 2, 255), ' ') = 0 AND INSTR(SUBSTRING(remain, 2, 255), '-') = 0 THEN
''
WHEN INSTR(SUBSTRING(remain, 2, 255), ' ') != 0 AND INSTR(SUBSTRING(remain, 2, 255), '-') = 0 THEN
SUBSTRING(SUBSTRING(remain, 2, 255), INSTR(SUBSTRING(remain, 2, 255), ' '), 255)
WHEN INSTR(SUBSTRING(remain, 2, 255), ' ') = 0 AND INSTR(SUBSTRING(remain, 2, 255), '-') != 0 THEN
SUBSTRING(SUBSTRING(remain, 2, 255), INSTR(SUBSTRING(remain, 2, 255), '-'), 255)
WHEN INSTR(SUBSTRING(remain, 2, 255), ' ') != 0 AND INSTR(SUBSTRING(remain, 2, 255), '-') != 0 THEN
SUBSTRING(SUBSTRING(remain, 2, 255), INSTR(SUBSTRING(remain, 2, 255), IF(INSTR(SUBSTRING(remain, 2, 255), ' ') < INSTR(SUBSTRING(remain, 2, 255), '-'), ' ', '-')), 255)
END AS remain,
idx + 1
FROM splitted_word_cte
WHERE remain != ''
),
formatted_word_cte AS (
SELECT content_id,
content_text,
idx,
word,
CONCAT(IF(delim = '^', '', delim), UPPER(SUBSTRING(word, 1, 1)), LOWER(SUBSTRING(word, 2, 255))) AS formatted_word
FROM splitted_word_cte
)
SELECT content_id,
content_text AS original_text,
GROUP_CONCAT(formatted_word ORDER BY idx SEPARATOR '') AS converted_text
FROM formatted_word_cte
GROUP BY 1, 2
ORDER BY 1;