2016-05-09 7 views
0

私は何をしようとしている(列の名前を動的に渡されたが、私はそれをハードコーディングされたので、それは単純に思われますこの質問)に:ダイナミックPIVOTテーブルのSQL Server 2012の

私は合計1つのフィールドピボットテーブルを使用してデータベースを照会し、SQL Server 2012のテーブルの行をカウントしようとしています、しかし、加えて、私はにしようとしているに合計を取得しますCOUNT()およびSUM()関数。私は合計(総合計)を含めるしたい

declare @campos nvarchar (max) 
 
select @campos = coalesce(@campos + ',[' + Setor + ']', '[' + Setor + ']') 
 
from dbo.TbFinanciamentos 
 
group by Setor 
 
order by Setor 
 

 
declare @resultado nvarchar(max) 
 
set @resultado = 
 
     'select * from(select Setor, ''br-'' + lower(UF_FILIAL) as [hc-key] from dbo.TbFinanciamentos) a 
 
\t \t pivot(
 
\t \t  count(Setor) 
 
\t \t \t for Setor in(' + @campos + ') 
 
\t \t ) a' 
 

 
execute(@resultado)
enter image description here しかし、:

通常、ピボットテーブルが(私が到達しようとしている何よりも簡単です)、このようになります。列として、私はthis tutorialに続き、すべてがうまくいった。私は(それが働いている)、これまで持って何

declare @campos nvarchar (max) 
 
select @campos = coalesce(@campos + ',[' + Setor + ']', '[' + Setor + ']') 
 
from dbo.TbFinanciamentos 
 
group by Setor 
 
order by Setor 
 

 
declare @total nvarchar(max) 
 
select @total = coalesce(@total + 'isnull([' + Setor + '], 0) + ', 'isnull([' + Setor + '], 0) + ') 
 
from dbo.TbFinanciamentos group by Setor order by Setor 
 
set @total = left(@total, len(@total) - 1) 
 

 
declare @resultado nvarchar(max) 
 
set @resultado = 
 
     'select *, '+ @total +' as [value] into #temp_total 
 
     from (select Setor, ''br-'' + lower(UF_FILIAL) as [hc-key] from dbo.TbFinanciamentos) a 
 
\t \t pivot(
 
\t \t  count(Setor) 
 
\t \t \t for Setor in(' + @campos + ') 
 
\t \t ) b 
 
     select * from #temp_total' 
 

 
execute(@resultado)
enter image description here しかし、私はCOUNT()のために行ったように、私はSUM(のためにやりたい)との両方で検索します同じクエリ。私はPIVOTの一部を複製し FULL OUTER JOINを実行しようとしましたが、問題は、発生するもの、最終的な結果に列を繰り返しているということです

  1. :私は、私が目指すものを行うために、これまでに試した何

    エラー(無効な列名)です。 @resultadoを印刷すると、列が重複しています。

    declare @campos nvarchar (max) 
     
    select @campos = coalesce(@campos + ',[' + Setor + ']', '[' + Setor + ']') 
     
    from dbo.TbFinanciamentos 
     
    group by Setor 
     
    order by Setor 
     
    
     
    declare @total nvarchar(max) 
     
    select @total = coalesce(@total + 'isnull([' + Setor + '], 0) + ', 'isnull([' + Setor + '], 0) + ') 
     
    from dbo.TbFinanciamentos group by Setor order by Setor 
     
    set @total = left(@total, len(@total) - 1) 
     
    
     
    declare @resultado nvarchar(max) 
     
    set @resultado = 
     
         'select *, '+ @total +' as [value] into #temp_total 
     
         from (
     
    \t \t  (select Setor, ''br-'' + lower(UF_FILIAL) as [hc-key] from dbo.TbFinanciamentos 
     
    \t \t \t \t pivot(
     
    \t \t \t \t \t count(Setor) 
     
    \t \t \t \t \t for Setor in(' + @campos + ') 
     
    \t \t \t \t ) as b 
     
    \t \t \t ) as sth 
     
    \t \t \t full outer join 
     
    \t \t \t (
     
    \t \t  select cast(Valor_do_Emprestimo as float) as Valor_do_Emprestimo, Setor, ''br-'' + lower(UF_FILIAL) as [hc-key] from dbo.TbFinanciamentos 
     
    \t \t \t \t pivot(
     
    \t \t \t \t \t count(Setor) 
     
    \t \t \t \t \t for Setor in(' + @campos + ') 
     
    \t \t \t \t ) as b 
     
    \t \t \t ) as sth_else 
     
    \t \t \t on sth.[hc-key] = sth_else.[hc-key] 
     
    \t \t ) 
     
         select * from #temp_total' 
     
    
     
    execute(@resultado)

私は同様に無効な列のエラーを生成 UNPIVOT and PIVOT methodに試み
  • +1

    あなたはどのフィールドを正確に加算していますか?ここにリストされているフィールドはすべてテキストフィールドです。 –

    +0

    お返事ありがとうございました。私はフィールド "Valor_do_Emprestimo"を合計しようとしています。私は '完全外部結合 'で試した別のクエリを投稿します。このクエリは、合計しようとしているフィールドを一覧表示します。 – StillBuggin

    +0

    @KyleHale私が試したことのあるアイテム1にクエリを追加しました – StillBuggin

    答えて

    1

    ダイナミックなことは、あなたが本当にメタデータを扱うことは決してないので、ダイナミックなことは非常に問題です。あなたは、CASE文を使用して、さまざまな施策を合体

    SUM(CASE When Setor = ''' + Setor ''' then 1 else 0 end) 
    as [' + Setor + '_Count], 
    SUM(CASE When Setor = ''' + Setor ''' then Valor_do_Emprestimo else 0 end) 
    as [' + Setor + '_Total],' 
    

    のようなもの、ちょうどこの方法あなたのデータセットに対して単一のクエリを構築するために、このような複数の条件付き集計を持っているときにどのような場合にはそれがより受け入れます。とにかく

    、あなたはこれら二つは、あなたが@camposと@totalのわずかに異なるバージョンを作成する必要があることを意味一意の列名を提供する必要が結合したい場合は、あなたの特定の問題に答えるために。ここで私はちょうどあなたにアイデアを与えるために@カムポスをしました。私もまた、列名の重複を避けるために、第2のピボットにhc_key2にhc_keyを変更しなければならなかった

    注意してください。

    declare @campos nvarchar (max) 
    select @campos = coalesce(@campos + ',[' + Setor + ']', '[' + Setor + ']') 
    from dbo.TbFinanciamentos 
    group by Setor 
    order by Setor 
    
    declare @campos2 nvarchar (max) 
    select @campos2 = coalesce(@campos2 + ',[' + Setor + '_2]', '[' + Setor + '_2]') 
    from dbo.TbFinanciamentos 
    group by Setor 
    order by Setor 
    
    declare @total nvarchar(max) 
    select @total = coalesce(@total + 'isnull([' + Setor + '], 0) + ', 'isnull([' + Setor + '], 0) + ') 
    from dbo.TbFinanciamentos group by Setor order by Setor 
    set @total = left(@total, len(@total) - 1) 
    
    declare @resultado nvarchar(max) 
    set @resultado = 
         'select * into #temp_total from (
         select *, '+ @total +' as [value]   from 
         (
          select * from (select Setor, ''br-'' + lower(UF_FILIAL) as [hc-key] from dbo.TbFinanciamentos) pvt 
           pivot(
            count([Setor]) 
            for Setor in(' + @campos + ') 
           ) as b 
          ) as sth 
          full outer join 
          (
           select *   from (
          select * from (select cast(Valor_do_Emprestimo as float) as Valor_do_Emprestimo, Setor+''_2'' as Setor, ''br-'' + lower(UF_FILIAL) as [hc-key2] from dbo.TbFinanciamentos) pvt 
           pivot(
            sum([Valor_do_Emprestimo]) 
            for Setor in(' + @campos2 + ') 
           ) as b 
          ) c 
          ) as sth_else 
          on sth.[hc-key] = sth_else.[hc-key2] 
         ) d 
    
         select * from #temp_total' 
         execute(@resultado) 
    
    +0

    これはchamのように機能します。ありがとう、カイル。あなたが私をたくさん助けてくれたことは言うまでもありません。ありがとうございました! – StillBuggin