Given the sample data and query below, I would like to know if it is possible to have the outcome be a single row, with the ChildTypeId, c.StartDate, c.EndDate being contained in the parent row. So, the outcome I'm hoping for based on the data below for ParentId = 1 would be:
1 2015-01-01 2015-12-31 AA 2015-01-01 2015-03-31 BB 2016-01-01 2016-03-31 CC 2017-01-01 2017-03-31 DD 2017-01-01 2017-03-31
Thanks for any help provided.
declare @parent table (Id int not null primary key, StartDate date, EndDate date)declare @child table (Id int not null primary key, ParentId int not null, ChildTypeId char(2) not null, StartDate date, EndDate date)
insert @parent select 1, '1/1/2015', '12/31/2015'
insert @child select 1, 1, 'AA', '1/1/2015', '3/31/2015'
insert @child select 2, 2, 'AA', '4/1/2015', '6/30/2015'
insert @child select 3, 3, 'AA', '7/1/2015', '9/30/2015'
insert @child select 4, 4, 'AA', '10/1/2015', '12/31/2015'
insert @parent select 2, '1/1/2016', '12/31/2016'
insert @child select 5, 1, 'BB', '1/1/2016', '3/31/2016'
insert @child select 6, 2, 'BB', '4/1/2016', '6/30/2016'
insert @child select 7, 3, 'BB', '7/1/2016', '9/30/2016'
insert @child select 8, 4, 'BB', '10/1/2016', '12/31/2016'
insert @parent select 3, '1/1/2017', '12/31/2017'
insert @child select 9, 1, 'CC', '1/1/2017', '3/31/2017'
insert @child select 10, 2, 'CC', '4/1/2017', '6/30/2017'
insert @child select 11, 3, 'CC', '7/1/2017', '9/30/2017'
insert @child select 12, 4, 'CC', '10/1/2017', '12/31/2017'
insert @parent select 4, '1/1/2018', '12/31/2018'
insert @child select 13, 1, 'DD', '1/1/2018', '3/31/2018'
insert @child select 14, 2, 'DD', '4/1/2018', '6/30/2018'
insert @child select 15, 3, 'DD', '7/1/2018', '9/30/2018'
insert @child select 16, 4, 'DD', '10/1/2018', '12/31/2018'
select p.Id, p.StartDate, p.EndDate, c.ChildTypeId, c.StartDate, c.EndDate
from @parent p
inner join @child c on c.ParentId = p.Id
where ParentId = 1
- Edited by Mike Smith1_1 8 hours 47 minutes ago