Teradata sql Analytics - выведен новый столбец для секционированного предложения - PullRequest
0 голосов
/ 21 февраля 2020

У меня есть таблица 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 |
+-------+------+------+-------+-------+

Ответы [ 4 ]

1 голос
/ 21 февраля 2020

Применить 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
1 голос
/ 21 февраля 2020

Это просто окно функций и 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;
0 голосов
/ 21 февраля 2020

Если вы хотите сделать это без 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

0 голосов
/ 21 февраля 2020

Вы можете использовать оконные функции. colaesce() пригодится для упрощения логики c здесь:

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