У меня есть таблица Book, и я хочу получить новый столбец word1, как показано ниже:
Book:
+-------+------+------+-------+
| name | page | line | word |
+-------+------+------+-------+
| Book1 | 1 | 1 | The |
+-------+------+------+-------+
| Book1 | 1 | 2 | A |
+-------+------+------+-------+
| Book1 | 1 | 3 | Time |
+-------+------+------+-------+
| Book1 | 1 | 4 | A |
+-------+------+------+-------+
| Book1 | 2 | 1 | An |
+-------+------+------+-------+
| Book1 | 2 | 2 | Tom |
+-------+------+------+-------+
| Book1 | 2 | 3 | A |
+-------+------+------+-------+
| Book1 | 3 | 1 | A |
+-------+------+------+-------+
| Book1 | 3 | 2 | Jack |
+-------+------+------+-------+
| Book1 | 3 | 3 | A |
+-------+------+------+-------+
| Book1 | 4 | 1 | Since |
+-------+------+------+-------+
| Book1 | 4 | 2 | They |
+-------+------+------+-------+
| Book1 | 4 | 3 | Sam |
+-------+------+------+-------+
Производный Word1 как
CASE
если какая-либо строка для той же страницы имеет 'The' then 'The'
, если какая-либо строка для той же страницы имеет 'An', тогда 'An'
, если какая-либо строка для той же страницы имеет 'A 'then' A '
ELSE
слово в строке 1
+-------+------+------+-------+-------+
| name | page | line | word | word1 |
+-------+------+------+-------+-------+
| Book1 | 1 | 1 | The | The |
+-------+------+------+-------+-------+
| Book1 | 1 | 2 | A | The |
+-------+------+------+-------+-------+
| Book1 | 1 | 3 | Time | The |
+-------+------+------+-------+-------+
| Book1 | 1 | 4 | A | The |
+-------+------+------+-------+-------+
| Book1 | 2 | 1 | An | An |
+-------+------+------+-------+-------+
| Book1 | 2 | 2 | Tom | An |
+-------+------+------+-------+-------+
| Book1 | 2 | 3 | A | An |
+-------+------+------+-------+-------+
| Book1 | 3 | 1 | A | A |
+-------+------+------+-------+-------+
| Book1 | 3 | 2 | Jack | A |
+-------+------+------+-------+-------+
| Book1 | 3 | 3 | A | A |
+-------+------+------+-------+-------+
| Book1 | 4 | 1 | Since | Since |
+-------+------+------+-------+-------+
| Book1 | 4 | 2 | They | Since |
+-------+------+------+-------+-------+
| Book1 | 4 | 3 | Sam | Since |
+-------+------+------+-------+-------+