sql server - Convert columns to rows by ID -
looking way convert columns rows in sql server.
i have table columns below:
[id] [action] [note] [resolution]
here want result columns: [id] [notes]
, result values be:
'1' 'action1' '1' 'note1' '1' 'resolution1' '2' 'action2' '2' 'note2' '2' 'note2.1' '2' 'resolution2' etc
any ideas how in t-sql? note field there multiple entries. thanks!
assuming source table , data looks this:
-- select * t: id action note resolution --- ------- ------- ----------- 1 action1 note1 resolution1 2 action2 note2 resolution2 2 action2 note2.1 resolution2
this query:
select distinct id, notes (select * t) source unpivot (notes ids in ([action], [note], [resolution]) ) unpivotted_table
will produce result:
id notes --- ------ 1 action1 1 note1 1 resolution1 2 action2 2 note2 2 note2.1 2 resolution2
which looks lot asking for.
you can find more information on how unpivot
operator works here.
Comments
Post a Comment