2016-05-09 52 views
0

我想要做的(列的名稱是動態過去了,但我硬編碼,因此它似乎更簡單在這個問題上):動態數據透視表SQL Server 2012中使用COUNT()和SUM()和Total列

我想查詢使用數據透視表數據庫來和一個字段和計數在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(做),並都在檢索相同的查詢。

我試過到目前爲止做什麼,我的目標是:

  1. 我複製樞紐部,並試圖做一個FULL OUTER JOIN,但問題是,它是重複的列到最後的結果是什麼產生一個錯誤(無效的列名稱)。當我打印@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」字段。我會發布另一個我用'full outer join'試過的查詢。此查詢列出了我試圖總結的字段。 – 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的結合。在這裏,我剛剛完成了@campos給你的想法。

    通知我也不得不hc_key改爲hc_key2在第二樞軸也避免了重複的列名。

    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