Teradata sql Analytics - 为分区子句派生了一个新列

Teradata sql Analytics - derived a new column for partitioned clause

我有一本书 table,我想导出一个新的列 word1,如下所示:

图书:

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

派生词 1 为
案例
如果同一页的任何一行有 'The' 那么 'The'
如果同一页的任何一行有 'An' 那么 'An'
如果同一页的任何一行有 'A' 那么 'A'
其他
第 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 |
+-------+------+------+-------+-------+

这只是一个 window 函数和 case:

select t.*,
       (case when sum(case when word = 'The' then 1 else 0 end) over (partition by page) > 0
             then 'The'
             when sum(case when word = 'An' then 1 else 0 end) over (partition by book, page) > 0
             then 'An'
             when sum(case when word = 'A' then 1 else 0 end) over (partition by book, page) > 0
             then 'A'
             else max(case when line = 1 then word end) over (partition by book, page)
        end) as derived_word            
from t;

您可以使用 window 函数。 colaesce() 可以方便地简化这里的逻辑:

select 
    b.*,
    coalesce(
        max(case when word = 'The' then word end) over(partition by name, page),
        max(case when word = 'An'  then word end) over(partition by name, page),
        max(case when word = 'A'   then word end) over(partition by name, page),
        max(case when line = 1     then word end) over(partition by name, page)
    ) word1
from book b

如果您希望在没有 window functions

的情况下执行此操作
select a.*, b.word1
from cte a
left join (select name, page ,coalesce(max(case word when 'the' then word end) 
                                      ,max(case word when'an' then word  end) 
                                      ,max(case word when 'a' then word end)
                                      ,max(case line when 1 then word end)) as word1
           from cte
           group by name, page) b on a.name=b.name and a.page=b.page;

DEMO

应用FIRST_VALUE并按优先顺序排序:

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