Search code examples
sqlteradatawindow-functions

Teradata sql Analytics - derived a new column for partitioned clause


I have a Book table, and i want to derived a new column word1 as below:

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   |
+-------+------+------+-------+

Derived Word1 as
CASE
if any of the line for same page has 'The' then 'The'
if any of the line for same page has 'An' then 'An'
if any of the line for same page has 'A' then 'A'
ELSE
word at line 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 |
+-------+------+------+-------+-------+

Solution

  • Apply FIRST_VALUE and order by priority:

    SELECT ...
       First_Value(word) 
       Over (PARTITION BY NAME, page
             ORDER BY 
                CASE word 
                  WHEN 'The' THEN 1
                  WHEN 'An'  THEN 2
                  WHEN 'A'   THEN 3
                  ELSE 99
                END, line)
    FROM tab