Sql server 正在尝试在SQL Server中进行求和(最大)计算
感谢你们提供的所有帮助,尽管这让我大开眼界,但不幸的是,它并没有产生我所期望的结果。为了更好地获得我所寻求的帮助,我将尝试解释我所希望实现的目标 我认为关注的主要栏目是“IN”、“AA_Now”、“STF_Now”、“dbo.Sheet1$.LOB_name”、“dbo.Sheet1$.LifeCycleName”和“dbo.Sheet1$.Area of Business”。每个“IN”都有一个“AA_Now”和“STF_Now”。一组“IN”在“dbo.Sheet1$.LOB_name”下汇总。在“dbo.Sheet1$.LOB_name”下,我只需要汇总的“IN”组的最大值。现在,“dbo.Sheet1$.LOB_name”被汇总到“dbo.Sheet1$.LifeCycleName”下,我想要的是“dbo.Sheet1$.LOB_name”下汇总的最大值的总和,以显示在“dbo.Sheet1$.LifeCycleName”的汇总中。最后,“dbo.Sheet1$.LifeCycleName”汇总到“dbo.Sheet1$.AreaOfBusiness”。和前面一样,我要寻找的是要显示的“dbo.Sheet1$.LifeCycleName”的总和。这些仅适用于“AA_Now”和“STF_Now”列 我尝试从数据透视表中进行此操作,但没有效果,我认为最好在原始数据中进行排序 我试图在SQLServer中进行求和(MAX)计算,并在执行命令时得到以下错误 味精130,15级,状态1,第6行 无法对包含聚合或子查询的表达式执行聚合函数 我确信这个错误是由这两个原因造成的 ,和(最大值(转换(浮动,替换([AA_Now],'N/A','0'))为[AA2_Now] 及 ,和(最大值(转换(浮动,替换([STF_Now],'N/A','0'))为[STF2_Now] 但是我不知道如何重写它而不引起错误 下面是完整的代码Sql server 正在尝试在SQL Server中进行求和(最大)计算,sql-server,sql-server-2014,Sql Server,Sql Server 2014,感谢你们提供的所有帮助,尽管这让我大开眼界,但不幸的是,它并没有产生我所期望的结果。为了更好地获得我所寻求的帮助,我将尝试解释我所希望实现的目标 我认为关注的主要栏目是“IN”、“AA_Now”、“STF_Now”、“dbo.Sheet1$.LOB_name”、“dbo.Sheet1$.LifeCycleName”和“dbo.Sheet1$.Area of Business”。每个“IN”都有一个“AA_Now”和“STF_Now”。一组“IN”在“dbo.Sheet1$.LOB_name”下汇
SELECT dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN
,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG
,dbo.CCA_Merged.regionId, dbo.CCA_Merged.sgcc
,convert(float,replace([SLC_Today],'N/A','0')) As [SLC_Today]
,convert(float,replace([AA_Now],'N/A','0')) As [AA_Now]
,SUM(MAX(convert(float,replace([AA_Now],'N/A','0')))) As [AA2_Now]
,convert(float,replace([SLCO_Today],'N/A','0')) As [SLCO_Today]
,convert(float,replace([CABN_Today],'N/A','0')) As [CABN_Today]
,convert(float,replace([COF_Today],'N/A','0')) As [COF_Today]
,convert(float,replace([HT_Today],'N/A','0')) As [HT_Today]
,convert(float,replace(replace([CH_Today],'N/A','0'),'-','0')) As [CH_Today]
,convert(float,replace([SLC_Now],'N/A','0')) As [SLC_Now]
,convert(float,replace([SLCO_Now],'N/A','0')) As [SLCO_Now]
,convert(float,replace([SLC_Thirty],'N/A','0')) As [SLC_Thirty]
,convert(float,replace(replace([SLCO_Thirty],'N/A','0'),'-','0')) As [SLCO_Thirty]
,convert(float,replace([ACWT_Today],'N/A','0')) As [ACWT_Today]
,convert(float,replace([CQ_Now],'N/A','0')) As [CQ_Now]
,convert(float,replace([LCQ_Now],'N/A','0')) As [LCQ_Now]
,convert(float,replace([SLCH_Now],'N/A','0')) As [SLCH_Now]
,convert(float,replace([STF_Now],'N/A','0')) As [STF_Now]
,SUM(MAX(convert(float,replace([STF_Now],'N/A','0')))) As [STF2_Now]
,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name
FROM dbo.Sheet1$ RIGHT OUTER JOIN
dbo.CCA_Merged ON dbo.Sheet1$.Skill_Name = dbo.CCA_Merged.lN
Group by ROLLUP (stf_now) ,dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN
,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG ,dbo.CCA_Merged.regionId
,dbo.CCA_Merged.sgcc,AA_Now,SLC_Today,SLCO_Today,CABN_Today,COF_Today,HT_Today,CH_Today
,SLC_Now,SLCO_Now,SLC_Thirty,SLCO_Thirty,ACWT_Today,CQ_Now,LCQ_Now,SLCH_Now
,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name
我是SQL Server的新手,非常感谢您的帮助
提前谢谢
更新的精简脚本
SELECT dbo.CCA_Merged.lN
,convert(float,replace([STF_Now],'N/A','0')) As [STF_Now]
,dbo.Sheet1$.LOB_name, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.AreaOfBusiness
FROM dbo.Sheet1$ RIGHT OUTER JOIN
dbo.CCA_Merged ON dbo.Sheet1$.Skill_Name = dbo.CCA_Merged.lN
Group by stf_now ,AA_Now,dbo.CCA_Merged.lN,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name
Order by AreaOfBusiness DESC
+----+---------+----------+---------------+----------------+
|在STF Now LOB name LifeCycleName业务领域|
+----+---------+----------+---------------+----------------+
|A | 46 | BSW | BS |业务|
|B | 46 | BSW | BS |业务|
|C | 0 | BOSS | BS |业务|
|D | 112 | MSD | BS |业务|
|电子商务|
|F | 42 | BHV | BR |业务|
|G | 23 | BCR | BR |业务|
|H | 23 | BHV | BR |业务|
|I | 55 | BSW2 | BS |业务|
|J | 1 | BSW2 | BS |业务|
|K | 46 | BSW | BS |业务|
|L | 112 | MSD | BS |业务|
|M | 112 | MSD | BS |业务|
|N | 57 | BSW | BS |业务|
|O | 0 | BOSS | BS |业务|
|P | 38 | MSD | BS |业务|
|Q | 38 | MSD | BS |业务|
|R | 19 | BHV | BR |业务|
|S | 0 | BCR | BR |业务|
|T | 19 | BHV | BR |业务|
|U | 2 | BSW | BS |业务|
|V | 1 | BSW | BS |业务|
|W | 57 | BSW | BS |业务|
|X | 38 | MSD | BS |业务|
|Y | 38 | MSD | BS |业务|
+----+---------+----------+---------------+----------------+
下面是添加的3列中的预期结果
LOB_名称2(这是LN现在产生的STF_的最大值)
57 BSW
0老板
112 MSD
42 BHV
公元前23年
55 BSW2
LifeCycleName2(这是LOB_Name2的最大汇总值之和)
224英国标准
65溴
业务领域2(这是LifeCycleName2汇总的总和)
289业务
您不能
sum
amax
,因为如果您有相同的分组,那么它无论如何都是相同的金额。您可能需要有一个具有不同分组依据的内部和外部部件,例如:
select
product_group,
sum(max_cost)
from
(
select
product,
product_group,
max(cost) as max_cost
from
orders
group by
product_group,product
) X
group by product_group
这个假想的SQL将获取每个产品的最大成本,并将它们相加到产品组级别。这是我唯一能计算出你实际上需要
sum
amax
的方法。你不能sum
amax
,因为不管怎样,如果你有相同的组别,它的金额是相同的。您可能需要有一个具有不同分组依据的内部和外部部件,例如:
select
product_group,
sum(max_cost)
from
(
select
product,
product_group,
max(cost) as max_cost
from
orders
group by
product_group,product
) X
group by product_group
这个假想的SQL将获取每个产品的最大成本,并将它们相加到产品组级别。这是我唯一能确定您实际上需要求和的方法。
sum
amax
您需要添加另一个查询级别来求和您的最大值
想法是在一次选择中最大化,然后使用外部查询对结果求和。我在下面的例子中使用了AVG和MAX,但是,可以使用任何聚合函数
SELECT
LocationID,
MaxAverageSalePriceByLocation=MAX(AvgerageSalePriceByUserLocation)
FROM
(
SELECT
UserID,
AvgerageSalePriceByUserLocation=AVG(SalePrice)
FROM
MyTable
GROUP BY
UserID,LocationID
)AS A
GROUP BY
LocationID
您需要添加另一个查询级别来求和最大值 想法是在一次选择中最大化,然后使用外部查询对结果求和。我在下面的例子中使用了AVG和MAX,但是,可以使用任何聚合函数
SELECT
LocationID,
MaxAverageSalePriceByLocation=MAX(AvgerageSalePriceByUserLocation)
FROM
(
SELECT
UserID,
AvgerageSalePriceByUserLocation=AVG(SalePrice)
FROM
MyTable
GROUP BY
UserID,LocationID
)AS A
GROUP BY
LocationID
如果要求所有行的总和
Max
值,请使用OVER()
如果要对每个组的所有行
Max
值求和,请使用OVER(Partition by)
注意将数字数据转换为浮点可能会导致近似值问题。。使用带精度和刻度的数值如果要对所有行的
Max
值求和,请使用OVER()
如果要对每个组的所有行
Max
值求和,请使用OVER(Partition by)
注意<
select
product_group,
sum(max_cost)
from
(
select
product,
product_group,
max(cost) as max_cost
from
orders
group by
product_group,product
) X
group by product_group
SELECT
LocationID,
MaxAverageSalePriceByLocation=MAX(AvgerageSalePriceByUserLocation)
FROM
(
SELECT
UserID,
AvgerageSalePriceByUserLocation=AVG(SalePrice)
FROM
MyTable
GROUP BY
UserID,LocationID
)AS A
GROUP BY
LocationID
Sum(Max(CONVERT(FLOAT, Replace([STF_Now], 'N/A', '0'))))OVER() AS [STF2_Now]
Sum(Max(CONVERT(FLOAT, Replace([STF_Now], 'N/A', '0'))))OVER(partition by grp1,grp2,..) AS [STF2_Now]
SELECT ...
, SUM(AA2_Now_Max)
...
, SUM(STF2_Now_Max)
FROM(
SELECT dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN
,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG
,dbo.CCA_Merged.regionId, dbo.CCA_Merged.sgcc
,convert(float,replace([SLC_Today],'N/A','0')) As [SLC_Today]
,convert(float,replace([AA_Now],'N/A','0')) As [AA_Now]
,MAX(convert(float,replace([AA_Now],'N/A','0'))) As [AA2_Now_Max]
,convert(float,replace([SLCO_Today],'N/A','0')) As [SLCO_Today]
,convert(float,replace([CABN_Today],'N/A','0')) As [CABN_Today]
,convert(float,replace([COF_Today],'N/A','0')) As [COF_Today]
,convert(float,replace([HT_Today],'N/A','0')) As [HT_Today]
,convert(float,replace(replace([CH_Today],'N/A','0'),'-','0')) As [CH_Today]
,convert(float,replace([SLC_Now],'N/A','0')) As [SLC_Now]
,convert(float,replace([SLCO_Now],'N/A','0')) As [SLCO_Now]
,convert(float,replace([SLC_Thirty],'N/A','0')) As [SLC_Thirty]
,convert(float,replace(replace([SLCO_Thirty],'N/A','0'),'-','0')) As [SLCO_Thirty]
,convert(float,replace([ACWT_Today],'N/A','0')) As [ACWT_Today]
,convert(float,replace([CQ_Now],'N/A','0')) As [CQ_Now]
,convert(float,replace([LCQ_Now],'N/A','0')) As [LCQ_Now]
,convert(float,replace([SLCH_Now],'N/A','0')) As [SLCH_Now]
,convert(float,replace([STF_Now],'N/A','0')) As [STF_Now]
,MAX(convert(float,replace([STF_Now],'N/A','0'))) As [STF2_Now_Max]
,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name
FROM dbo.Sheet1$ RIGHT OUTER JOIN
dbo.CCA_Merged ON dbo.Sheet1$.Skill_Name = dbo.CCA_Merged.lN
Group by ROLLUP (stf_now) ,dbo.CCA_Merged.id, dbo.CCA_Merged.timeStamp, dbo.CCA_Merged.name, dbo.CCA_Merged.lN
,dbo.CCA_Merged.type, dbo.CCA_Merged.id2, dbo.CCA_Merged.aG ,dbo.CCA_Merged.regionId
,dbo.CCA_Merged.sgcc,AA_Now,SLC_Today,SLCO_Today,CABN_Today,COF_Today,HT_Today,CH_Today
,SLC_Now,SLCO_Now,SLC_Thirty,SLCO_Thirty,ACWT_Today,CQ_Now,LCQ_Now,SLCH_Now
,dbo.Sheet1$.AreaOfBusiness, dbo.Sheet1$.LifeCycleName, dbo.Sheet1$.LOB_name ) x
GROUP BY ...