Friday, 15 March 2013

sql - how to make the field value to columns -



sql - how to make the field value to columns -

i have table below.

+----------+ | tools | +----------+ | pencil | | eraser | | notebook | | pencil | | notebook | | notebook | +----------+

can transform this?

+---------+----------+---------+ | no name | no name | no name | <-- column header +---------+----------+---------+ | pencil | notebook | eraser | <-- values +---------+----------+---------+

tried pivot query don't think got right. tools may not fixed. mean, maybe add together tools it. help much appreciated. thanks

declare @t table(tools varchar(50)) insert @t values ('pencil'),('eraser'),('notebook'),('pencil'),('notebook'),('notebook') select * @t select * ( select tools @t grouping tools ) t pivot ( max(tools) tools in(pencil, eraser, notebook) ) p;

sql sql-server

No comments:

Post a Comment