c# - How should I 'rotate' or 'flatten' this research data? PIVOT, self join or something else? -
i'm having hard time finding examples close i'm doing, or i'm not understanding examples i'm finding.
i have research database contains persons responses multiple questions @ different points in time. 'admin#' below represents "administration" of test data represents. or can think of containing 'time' test given, example, time1, time2, time3
respondentid# admin# question1 question2 question3 question4 question5 1 1 b c d e 1 2 e d c b 1 3 q w e r t 2 1 z x c v b 2 2 p o u y 2 3 y h n u j
what need arrange data each set of responses particular respondent in same row. we'd take 5 question fields , turn them 15 question fields,
respondentid# admin1question1 admin1question2 admin1question3 admin1question4 admin1question5 admin2question1 admin2question2 admin2question3 admin2question4 admin2question5 admin3question1 admin3question2 admin3question3 admin3question4 admin3question5
as can see, every field begins admin1 correspond row in example above has admin# value of 1.
please forgive me if i'm not explaining properly.
to further complicate matters, maximum number of "administrations" or "times" can increase in future. 3, possible same test administered 4, 5 or more times in future. whatever solution used problem can static , updated hand account additional "times" in future, awesome if solution dynamically accounted unspecified number of "times".
this data stored in ms sql 2005 database, tsql option, if better solution exists in c# or linq (the overall project asp.net app), i'm open well. whatever think works best! :)
thanks reading question!
the basic approach i'm following manually pivot using code like:
select respondentid, min(case when admin=1 question1 else null end) admin1_question1, min(case when admin=2 question1 else null end) admin2_question1, min(case when admin=3 question1 else null end) admin3_question1 tests group respondentid
so, using dynamic t-sql statement build , execute query follows:
declare @select varchar(max) select @select = coalesce(@select+',','')+ 'min(case when admin='+a+' '+q+' else null end) [admin'+a+'_'+q+']' (select distinct cast(adminas varchar(10)) tests) p1 cross join ( select 'question1' q union select 'question2' union select 'question3' union select 'question4' union select 'question5' ) p2 order a, q declare @sql varchar(max) set @sql = 'select respondentid, '+@select+' tests group respondentid' execute(@sql)
it's not dynamic t-sql solution available, should work!
Comments
Post a Comment