对sql中的列求和并按其分组
基本上我有下面的问题。我试图对第二列、第三列和第四列进行求和。我按ShipToAddressNo和shiptoname进行分组。是否可以使用原始quantityshipped和extendedprice值计算出第四列中的等式?由于我正在对列求和以使查询工作,因此计算被关闭对sql中的列求和并按其分组,sql,group-by,Sql,Group By,基本上我有下面的问题。我试图对第二列、第三列和第四列进行求和。我按ShipToAddressNo和shiptoname进行分组。是否可以使用原始quantityshipped和extendedprice值计算出第四列中的等式?由于我正在对列求和以使查询工作,因此计算被关闭 declare @rundate datetime set @rundate = '3/11/2013' Declare @Sales table (ShipToAddressNo int, ShipToName varc
declare @rundate datetime
set @rundate = '3/11/2013'
Declare @Sales table (ShipToAddressNo int, ShipToName varchar(40), SumOfQuantityShipped int,
SumOfAmountShipped money, TotalDeductions float)
INSERT INTO @Sales
SELECT
ShipToAddressNo,
ShipToName,
sum(QuantityShipped),
sum(ExtendedPrice),
(p.[Transfer Price] * QuantityShipped) +
(p.[Profit Split Calculation 1 (Prasco Distribution Allowance)] * QuantityShipped) +
(p.[Profit Split Calculation 2 (Share to Partner)] * QuantityShipped) +
(p.[Profit Split Calculation 3 (Cost Adjustment)] * QuantityShipped) +
(p.[Profit Split Calculation (Cost Adjustment)] * QuantityShipped) +
(p.[Net Sales Profit Split] * QuantityShipped) TotalDeductions --Sum this entire line
FROM
SalesSummary ss join [Product] p
on ss.ShortItemNo = p.SDITM
join JDE_PRODUCTION.PRODDTA.F4101 im
on im.IMITM = p.SDITM
WHERE
InvoiceDate = @RunDate
GROUP BY
ShipToAddressNo,
ShipToName
这是你想要的吗
sum((p.[Transfer Price] * QuantityShipped) +
(p.[Profit Split Calculation 1 (Prasco Distribution Allowance)] * QuantityShipped) +
(p.[Profit Split Calculation 2 (Share to Partner)] * QuantityShipped) +
(p.[Profit Split Calculation 3 (Cost Adjustment)] * QuantityShipped) +
(p.[Profit Split Calculation (Cost Adjustment)] * QuantityShipped) +
(p.[Net Sales Profit Split] * QuantityShipped)
) as TotalDeductions --Sum this entire line