对于我的报告,我需要将总销售额减去成本以获得利润。 我尝试了所有不同的表达方式,但我没有运气。
要查找总销售额,我将权重乘以单位成本。
例:
[IC_ProductLots].[OriginalQuantity_Stk] *[ICP].[UnitCost]
Total Weight = 1,000
Unit Cost = 3
Total Sales = 3,000
我需要了解如何将总销售额存储为变量,然后能够将其用作以下表达式:
Max(@TotalSalesVariable - (@PurchaseCost + @Prod_CostLBS) * @InputWeight) AS [Profit]
目前我正在使用表达式:
AVG([ICP].[UnitCost]) OVER (PARTITION BY [ARC].[CustomerCode])
* SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk]))
OVER (PARTITION BY [ARC].[CustomerCode])
- ( @PurchaseCost + @Prod_CostLBS ) * @InputWeight
但这不起作用,因为AVG
单位成本不能产生正确的数字。因此,如果我使用变量来存储总销售额,我开始认为我只能计算利润。
Catch,该变量不能包含WHERE
语句,因为WHERE
由运行报告时的用户输入决定。
我正在使用Microsoft SQL Sever 2005
我的完整代码发布如下: 我在实施@CostOfSales
的评论后更新了它SET NOCOUNT ON;
DECLARE @PurchaseCost Decimal(19,8);
DECLARE @InputWeight Decimal(19,8);
DECLARE @Prod_CostLBS Decimal(19,8);
DECLARE @CostOfSales Decimal(19,8);
SET @PurchaseCost = 2.58;
SET @InputWeight = 18100;
SET @Prod_CostLBS = .15;
SET @CostOfSales = (@PurchaseCost + @Prod_CostLBS) * @InputWeight;
SELECT
CAST([ARC].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([ARC].[Name] AS NVARCHAR(40)) AS [Supplier]
, [PC].ProductCode
, [PC].Description1
, Count(IC_ProductLots.OriginalQuantity_Alt) AS [Boxes]
, IC_ProductLots.UnitOfMeasure_Alt
, Sum(IC_ProductLots.OriginalQuantity_Stk) AS [Weight]
, IC_ProductLots.UnitOfMeasure_Stk
, [ICP].UnitCost AS [Unit Cost]
, Sum([IC_ProductLots].[OriginalQuantity_Stk] *[ICP].[UnitCost]) AS [Total Sales]
, Avg(([IC_ProductLots].[OriginalQuantity_Stk] / [IC_ProductLots].[OriginalQuantity_Alt])) AS [Avg. Box Weight]
, Sum([IC_ProductLots].[OriginalQuantity_Stk] / @InputWeight) AS [Yield]
, @InputWeight - SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk])) OVER () AS [Shrink]
, Max((@PurchaseCost + @Prod_CostLBS) * @InputWeight) AS [Cost]
, SUM([IC_ProductLots].[OriginalQuantity_Stk] * [ICP].[UnitCost]) OVER (PARTITION BY [ARC].[CustomerCode]) - @CostOfSales AS [Profit]
, @CostOfSales AS [CostOfSales]
FROM (((( IC_Products [PC]
INNER JOIN DC_Transactions [DCT]
ON [PC].ProductKey = [DCT].ProductKey)
INNER JOIN AR_Customers [ARC]
ON [DCT].CustomerKey = [ARC].CustomerKey)
INNER JOIN IC_ProductLots
ON [DCT].LotKey = IC_ProductLots.LotKey)
LEFT OUTER JOIN IC_ProductCosts [ICP]
ON ICP.ProductKey=PC.ProductKey and ICP.ProductCostCode=5)
WHERE
(IC_ProductLots.ProductionDate >= { ts '2015-06-24 00:00:00' } AND (IC_ProductLots.ProductionDate <= { ts '2015-06-24 00:00:00' } OR IC_ProductLots.ProductionDate Is Null))
AND ((1=1) AND [ARC].CustomerKey IN (124) )
GROUP BY
CAST([ARC].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([ARC].[Name] AS NVARCHAR(40))
, [PC].ProductCode
, [PC].Description1
, IC_ProductLots.UnitOfMeasure_Alt
, IC_ProductLots.UnitOfMeasure_Stk
, [ICP].UnitCost
, IC_ProductLots.ProductionDate
, [ARC].CustomerKey
, [ARC].CustomerCode
ORDER BY
CAST([ARC].[CustomerCode] AS NVARCHAR(40)) + ' - ' + CAST([ARC].[Name] AS NVARCHAR(40))
, @InputWeight - SUM(Sum([IC_ProductLots].[OriginalQuantity_Stk])) OVER ()
, Max((@PurchaseCost + @Prod_CostLBS) * @InputWeight)
, SUM([IC_ProductLots].[OriginalQuantity_Stk] * [ICP].[UnitCost]) OVER (PARTITION BY [ARC].[CustomerCode]) - @CostOfSales