если записи идентичны, отображать другую информацию в новых столбцах - PullRequest
0 голосов
/ 10 апреля 2019

Я хочу видеть данные второй строки, такие как дата начала, дата окончания и связываться с новыми столбцами в одной строке, если они совпадают.

Name    Id  Start date  End date    Association
XYZ 100 1/1/2017    1/1/2022    Marketing
XYZ 100 5/1/2018    1/1/2028    Business

Результат:

Name    Id  Start date  End date    Association Start date1 End date1   Association1
XYZ 100 1/1/2017    1/1/2022    Marketing   5/1/2018    1/1/2028    

Бизнес enter image description here

1 Ответ

1 голос
/ 11 апреля 2019

Ваша проблема решена: -

select Id,
  name,
  max(case when rn = 1 then StartDate end) StartDate,
  max(case when rn = 1 then EndDate end) EndDate,
  max(case when rn = 1 then Association end) Association,
  max(case when rn = 2 then StartDate end) StartDate1,
  max(case when rn = 2 then EndDate end) EndDate1,
  max(case when rn = 2 then Association end) Association1
from
(
  select id, name, StartDate, EndDate, Association,
    row_number() over(partition by Id order by name) rn
  from Business
) src
group by id, name;

OutPut and Tested Code

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...