Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对sql中的列求和并按其分组_Sql_Group By - Fatal编程技术网

对sql中的列求和并按其分组

对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

基本上我有下面的问题。我试图对第二列、第三列和第四列进行求和。我按ShipToAddressNo和shiptoname进行分组。是否可以使用原始quantityshipped和extendedprice值计算出第四列中的等式?由于我正在对列求和以使查询工作,因此计算被关闭

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