How to route rows to columns?

0

Already answered by Motta, thanks for your patience.

How can I transpose rows to columns in SQLServer, so that something like this:

Keepthisformat?

    
asked by anonymous 29.07.2016 / 22:26

3 answers

4

With UNION maybe

select 'jan' , jan linhabase from tabela 
union
select 'fev' , fev linhabase from tabela 
union
...
select 'dez' , dezlinhabase from tabela 

But the problem was unclear to me, another thing is what seems like a bad model to me.

Also search for UNPIVOT

    
01.08.2016 / 17:55
4

A slightly more elegant shape than it was presented, would be to use unpivot ,

select *
from Tabela
unpivot
(
  Valor
  for Ano in ([Jan], [Fev], [Mar], [Abr], [Mai], [Jun], [Jul], [Dez]) unpiv
order by ano desc
    
02.08.2016 / 13:10
0

If you want to count the values. Use SUM Example ..

SELECT sum(Jan), count(Fev), sum(Mar),..... FROM tabela.

Result will be the sum of the rows in the columns in January, February, etc. ...

    
29.07.2016 / 22:35