试图让我的SQL数据到Excel中,NULL聚合警告,无法连接

我已经创build了一个查询,只要结果如实,我就可以得到我想要的结果。理想情况下,我只想创build一个视图并将其带入Excel以供使用。

这是代码…..

--Calculates the percentage split of Property Support Departments for YTD Oct/Nov/Dec SET ANSI_WARNINGS OFF; declare @PropApportion Table( type varchar(50), net money); insert into @PropApportion select type,Net from DB_ConsolApportionment where type in ('PostComplete','DirectPropSupPbo','PropSupPbo') declare @PostComplete as money set @PostComplete = (select net from @PropApportion where type = 'PostComplete') declare @PropSup as money set @PropSup = (select net from @PropApportion where type = 'PropSupPbo') declare @DirPropSup as money set @DirPropSup = (select net from @PropApportion where type = 'DirectPropSupPbo') ----------------------------------------------------------------------------------------------- select type, Concat, Net, Cast(1. * Net/ SUM(Net) OVER(PARTITION BY type)as decimal(8,3)) as '%', round(@PostComplete * ((1. * Net/ SUM(Net) OVER(PARTITION BY type))),2) as 'PostCompleteSplit', round(@PropSup * ((1. * Net/ SUM(Net) OVER(PARTITION BY type))),2) as 'PropSupSplit', round(@DirPropSup * ((1. * Net/ SUM(Net) OVER(PARTITION BY type))),2) as 'DirPropSupSplit' from (select type, Concat, sum(-[oct-15]-[Nov-15]-[Dec-15]) as 'Net' from DB_ConsolApportionment ca where type = 'Sales' and ((ca.Concat like '%Estate%' or ca.Concat like '%Developer%') or concat like 'Commercial') group by concat, type )ca where type = 'Sales' and ((ca.Concat like '%Estate%' or ca.Concat like '%Developer%') or concat like 'Commercial') order by case when concat like '%Commercial%' then 1 when concat like '%EstateAgentsA%' then 2 when concat like '%EstateAgentsB%' then 3 when concat like '%DeveloperA%' then 4 when concat like '%DeveloperB%' then 5 when concat like '%DeveloperC%' then 6 end; 

这给了我这些结果….

 type Concat Net % PostCompleteSplit PropSupSplit DirPropSupSplit Sales Commercial 46366.67 0.101 3282.290000 443.220000 11857.150000 Sales EstateAgentsA 69717.00 0.152 4935.250000 666.420000 17828.430000 Sales EstateAgentsB 146788.94 0.321 10391.160000 1403.150000 37537.700000 Sales DeveloperA 88731.19 0.194 6281.260000 848.180000 22690.840000 Sales DeveloperB 50324.18 0.110 3562.440000 481.050000 12869.190000 Sales DeveloperC 55371.69 0.121 3919.750000 529.300000 14159.960000 

这正是我所需要的,但是我需要把它放到一个视图中,这样我才能快速将其导入到Excel中。 如果我直接将查询粘贴到Excel(数据/现有连接等),最初我得到了'空集合'的警告,然后我添加.. SET ANSI_WARNNINGS OFF; 删除警告,因为我有几个其他的视图从同一个来源运行,从视图运行时没有问题。 一旦警告被closures,查询就不会连接到服务器。

有没有更好的方式来构build我的代码,所以我可以将其存储为一个视图? …我应该使用子查询,而不是variables?

修正它我自己,我会张贴这反正万一别人觉得有用(或者如果有人有任何其他技巧关于我的代码(..将不胜感激))。

所以..subqueries而不是variables,它需要10秒运行,但它的工作原理!

 --Calculates the percentage split of Peterborough Property Support Departments for YTD Oct/Nov/Dec select top 999999999999 ca.Type, ca.Concat, ca.Net, Cast(1. * ca.Net/ SUM(ca.Net) OVER(PARTITION BY ca.type)as decimal(8,3)) as '%', round((select sum(net) from DB_ConsolApportionment where type = 'PostComplete' ) * ((1. * ca.Net/ SUM(ca.Net) OVER(PARTITION BY ca.type))),2) as 'PostCompleteSplit', round((select sum(net) from DB_ConsolApportionment where type = 'PropSupPbo' ) * ((1. * ca.Net/ SUM(ca.Net) OVER(PARTITION BY ca.type))),2) as 'PropSupSplit', round((select sum(net) from DB_ConsolApportionment where type = 'DirectPropSupPbo' ) * ((1. * ca.Net/ SUM(ca.Net) OVER(PARTITION BY ca.type))),2) as 'DirPropSupSplit' from (select type, Concat, sum(-[oct-15]-[Nov-15]-[Dec-15]) as 'Net' from DB_ConsolApportionment ca where type = 'Sales' and ((Concat like '%Estate%' or Concat like '%Developer%') or concat like 'Commercial') group by concat, type )ca where ca.Type = 'Sales' and ((ca.Concat like '%Estate%' or ca.Concat like '%Developer%') or ca.concat like 'Commercial') order by case when ca.Concat like '%Commercial%' then 1 when ca.Concat like '%EstatsAgentsA%' then 2 when ca.concat like '%EstateAgentsB%' then 3 when ca.concat like '%DeveloperA%' then 4 when ca.concat like '%DeveloperB%' then 5 when ca.concat like '%DeveloperC%' then 6 end;