试图将我的SQL数据导入Excel,NULL聚合警告并且无法连接

时间:2016-02-11 12:12:31

标签: sql-server excel

我已经创建了一个查询,根据结果,我可以准确地提供我想要的内容。理想情况下,我只想创建一个视图并将其带入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(数据/现有连接等)中,最初我得到了'Null Aggregate'警告,然后我添加了..     SET ANSI_WARNNINGS OFF; 删除警告,因为我有几个其他视图从同一来源运行,从视图运行时没有问题。关闭警告后,查询将无法连接到服务器。

是否有更好的方法来构建我的代码,以便将其存储为视图? ...我应该使用子查询而不是变量吗?

1 个答案:

答案 0 :(得分:1)

我自己修正了,无论如何我会发布这个以防其他人觉得它有用(或者如果有人对我的代码有任何其他提示(...会非常感激))。

所以..subqueries而不是变量,运行需要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;