Решение «Все в одном». Для получения необходимых данных набор результатов должен быть отфильтрован по столбцу SCENARIO_ID.
with
test as (
select
*
from (
values (123, 'EXTERNAL'), (123, 'INTERNAL'),
(456, 'INTERNAL'), (456, 'IBM'), (456, 'DELL'),
(345, 'EXTERNAL'), (345, 'EXTERNAL'), (345, 'EXTERNAL'),
(434, 'INTERNAL'), (434, 'US'), (434, 'US'), (434, 'EXTERNAL'),
(567, 'INTERNAL'), (567, 'EXTERNAL'), (567, 'EXTERNAL'), (567, 'IBM'),
(121, 'INTERNAL'), (121, 'INTERNAL'), (121, 'INTERNAL'),
(131, 'EXTERNAL'), (131, 'IBM')
) t(cola, colb)
),
t as (
select
cola,
count(*) qnt,
count(distinct colb) distinct_qnt,
sum(iif(colb = 'EXTERNAL', 1, 0)) external_qnt
from test
group by cola
)
select
cola,
iif(external_qnt = 0 or external_qnt = qnt,
3, iif(external_qnt = 1 and distinct_qnt = qnt, 1, 2)) scenario_id
from t;
Выход:
+------+-------------+
| cola | scenario_id |
+------+-------------+
| 121 | 3 |
| 123 | 1 |
| 131 | 1 |
| 345 | 3 |
| 434 | 2 |
| 456 | 3 |
| 567 | 2 |
+------+-------------+
Проверьте это онлайн с Rextester .