[英]Dynamic PIVOT table SQL Server 2012 with COUNT() and SUM() and a TOTAL column
我正在嘗試做的事情(列名是動態傳遞的,但是我對其進行了硬編碼,因此在此問題中看起來更簡單):
我正在嘗試使用PIVOT表查詢數據庫以求和一個字段並計算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 pivot( count(Setor) for Setor in(' + @campos + ') ) a' execute(@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 (select Setor, ''br-'' + lower(UF_FILIAL) as [hc-key] from dbo.TbFinanciamentos) a pivot( count(Setor) for Setor in(' + @campos + ') ) b select * from #temp_total' execute(@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 ( (select Setor, ''br-'' + lower(UF_FILIAL) as [hc-key] from dbo.TbFinanciamentos pivot( count(Setor) for Setor in(' + @campos + ') ) as b ) as sth full outer join ( select cast(Valor_do_Emprestimo as float) as Valor_do_Emprestimo, Setor, ''br-'' + lower(UF_FILIAL) as [hc-key] from dbo.TbFinanciamentos pivot( count(Setor) for Setor in(' + @campos + ') ) as b ) as sth_else on sth.[hc-key] = sth_else.[hc-key] ) select * from #temp_total' execute(@resultado)
因此,不用說進行任何動態處理都是非常有問題的,因為您永遠無法真正掌握元數據。 在任何情況下,當您有多個這樣的條件聚合來使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.