<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Betoxz - Criar Pivot Tables</Title>
      <Shortcut>Betoxz-CriarPivotTables</Shortcut>
      <Description>Betoxz - Criar Pivot Tables [T-SQL]</Description>
      <Author>carlos martins</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=99f9ed70-b466-4a30-8bed-109091fa24b1</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="T-SQL"><![CDATA[/*******************Este exmplo é para versão do Sql 2000*********************************/
/*****************************************************************************************/
select  year(DataCriacao) Ano
         , Janeiro = sum(case when month(DataCriacao)=1 then Total end) 
         , Fevereiro = sum(case when month(DataCriacao)=2 then Total end) 
         , Março = sum(case when month(DataCriacao)=3 then Total end)
         , Outubro = sum(case when month(DataCriacao)=10 then Total end) 
from pedido 
group by year(DataCriacao) 
order by year(DataCriacao);
/*****************************************************************************************/
				/*********************************************/

/*******************Este exmplo é para versão do Sql 2005*********************************/
/*****************************************************************************************/
Select Ano
    , [1] as Janeiro
	, [2] as Fevereiro
	, [3] as Março 
	, [10] as Outubro 
from (
	select year(DataCriacao) Ano,month(DataCriacao) Mes, Total from pedido
	)Pedido 
	pivot (sum(Total) for Mes in ([1],[2],[3],[10])) PedidoPivot
order by 1;
/*****************************************************************************************/
				/*********************************************/
]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>