sql - How to capture different values in the same column with a similar ID into one row with specific column indicators -
i need capture yes or no answer question codes columns serve indicator each specific question code. question codes set values , sake of example can 'a', 'b', or 'c'.
any appreciated, looking forward answers sql gurus can think of!
example input table:
id question code yesorno 1 yes 1 b no 1 c no 2 no 2 b yes 2 c yes 3 no 3 b no 3 c yes
desired table/view:
id a_answer b_answer c_answer 1 yes no no 2 no yes yes 3 no no yes
to clarify in sql server.
you can perform conditional aggregation:
select id, a_answer = max(case when [question code] = 'a' yesorno end), b_answer = max(case when [question code] = 'b' yesorno end), c_answer = max(case when [question code] = 'c' yesorno end) tbl group id;
Comments
Post a Comment