To get the result, I would first turn off and then rotate your data. The non-repetition will take the date and diagnostic columns and convert them to strings. Once the data is in rows, then you can apply a support bar.
If you have a known number of values, you can program your query in the same way:
select * from ( select person, [case#], age, col+'_'+cast(rn as varchar(10)) col, value from ( select person, [case#], age, diagnosis, convert(varchar(10), diagnosisdate, 101) diagnosisDate, row_number() over(partition by person, [case#] order by DiagnosisDate) rn from yourtable ) d cross apply ( values ('diagnosis', diagnosis), ('diagnosisDate', diagnosisDate) ) c (col, value) ) t pivot ( max(value) for col in (diagnosis_1, diagnosisDate_1, diagnosis_2, diagnosisDate_2, diagnosis_3, diagnosisDate_3, diagnosis_4, diagnosisDate_4) ) piv;
See SQL Fiddle with Demo .
I am going to assume that you will have an unknown number of diagnosis values โโfor each case. If so, then you will need to use dynamic sql to generate the result:
DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) select @cols = STUFF((SELECT ',' + QUOTENAME(col+'_'+cast(rn as varchar(10))) from ( select row_number() over(partition by person, [case#] order by DiagnosisDate) rn from yourtable ) t cross join ( select 'Diagnosis' col union all select 'DiagnosisDate' ) c group by col, rn order by rn, col FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set @query = 'SELECT person, [case#], age,' + @cols + ' from ( select person, [case#], age, col+''_''+cast(rn as varchar(10)) col, value from ( select person, [case#], age, diagnosis, convert(varchar(10), diagnosisdate, 101) diagnosisDate, row_number() over(partition by person, [case#] order by DiagnosisDate) rn from yourtable ) d cross apply ( values (''diagnosis'', diagnosis), (''diagnosisDate'', diagnosisDate) ) c (col, value) ) t pivot ( max(value) for col in (' + @cols + ') ) p ' execute(@query);
See SQL Fiddle with Demo . Both queries give the result:
| PERSON | CASE# | AGE | DIAGNOSIS_1 | DIAGNOSISDATE_1 | DIAGNOSIS_2 | DIAGNOSISDATE_2 | DIAGNOSIS_3 | DIAGNOSISDATE_3 | DIAGNOSIS_4 | DIAGNOSISDATE_4 | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | John | 13784 | 56 | Depression | 03/13/2012 | Brain Injury | 03/14/2012 | Spinal Cord Injury | 03/15/2012 | Hypertension | 03/16/2012 | | Kate | 2643 | 37 | Bipolar | 03/11/2012 | Hypertension | 03/12/2012 | (null) | (null) | (null) | (null) | | Kevin | 500934 | 25 | Down Syndrome | 03/18/2012 | Clinical Obesity | 03/19/2012 | (null) | (null) | (null) | (null) | | Pete | 803342 | 34 | Schizophenia | 03/17/2012 | (null) | (null) | (null) | (null) | (null) | (null) |