#sql #oracle
#sql #Oracle
Вопрос:
У меня есть эта таблица:
ID ACTIVITY START_DT END_DT
1 A 01-01-20 05-01-20
1 B 06-01-20 10-01-20
2 A 11-01-20 15-01-20
2 B 16-01-20 22-01-20
3 A 06-01-20 09-01-20
3 B 13-01-20 16-01-20
Я хочу дату начала A и дату окончания B, вот так:
ID START_DT END_DT
1 01-01-20 10-01-20
2 11-01-20 22-01-20
3 06-01-20 16-01-20
я сделал это:
select
id,
case when id = 'a' then start_dt end start_dt,
case when id = 'b' then end_dt end end_dt
from table
я понимаю это
id start_date end_date
1 01-01-20 null
1 null 10-01-20
2 11-01-20 null
2 null 22-01-20
3 06-01-20 null
3 null 16-01-20
Ответ №1:
Используйте агрегацию:
select id,
max(case when id = 'a' then start_dt end) as start_dt,
max(case when id = 'b' then end_dt end) as end_dt
from table
group by id;