I have the following tables:
declare @Risks table(id int identity, name varchar(20))
declare @Mitigations table(id int identity, riskId int, mitigation_desc varchar(20), record_date datetime)
insert into @Risks(name)
select 'Risk 1' union all
select 'Risk 2' union all
select 'Risk 3'
insert into @Mitigations(riskId, mitigation_desc, record_date)
select 1, 'Mitigation 1', '2016-01-01' union all
select 1, 'Mitigation 2', '2016-01-01' union all
select 2, 'Mitigation 3', '2016-01-01'
SELECT *
FROM @Risks a
LEFT OUTER JOIN @Mitigations b ON a.id = b.riskId
Each risk can have more than 1 mitigation, there is no limit, what I would like to do is to be able to display all of the mitigations of each risk horizontally like this:
ID Name mitigation_desc record_date mitigation_desc record_date
--- ----- ------------------ ------------- ---------------- -------------
1 Risk 1 Mitigation 1 2016-01-01 Mitigation 2 2016-01-01
2 Risk 2 Mitigation 3 2016-01-01 NULL NULL
3 Risk 3 NULL NULL NULL NULL
I have tried a few scenarios using pivots and joins but none of them is flexible enough as a risk can have number of mitigation. Is there any other way of achieving this?
To make things easier, I could restrict the number of mitigations displayed, for instance always display 3 sets of mitigations per risk.
Thanks