案例陈述中的总和

时间:2018-03-23 09:50:15

标签: sql sql-server

我试图在满足条件时计算字段。我已经玩了好几个小时,但没有运气:(。

所以我需要找到并计算每个月的保证金。我为销售做了这个简单的

SUM(CASE when CalendarMonth = 1 then TotalSales else 0 end) as 'January',

然而,当试图计算保证金时,我经常得到错误 '无法对包含聚合或子查询的表达式执行聚合函数。'

我是以完全错误的方式解决这个问题吗?

    Declare @Sales numeric, @Cost numeric, @Margin Numeric

    Set @Sales = (Select SUM(TotalSales)
            From SalesAnalysis2)

    Set @Cost = (Select SUM(TotalCost)
            From SalesAnalysis2)

    Set @Margin = ((@Sales - @Cost) / @Sales)

    Select distinct

    Case CalendarMonth when 1 then @Margin else 0 end as 'January',
    Case CalendarMonth when 2 then @Margin else 0 end as 'Febuary',
    Case CalendarMonth when 3 then @Margin else 0 end as 'March',
    Case CalendarMonth when 4 then @Margin else 0 end as 'April',
    Case CalendarMonth when 5 then @Margin else 0 end as 'May',
    Case CalendarMonth when 6 then @Margin else 0 end as 'June',
    Case CalendarMonth when 7 then @Margin else 0 end as 'July',
    Case CalendarMonth when 8 then @Margin else 0 end as 'August',
    Case CalendarMonth when 9 then @Margin else 0 end as 'September',
    Case CalendarMonth when 10 then @Margin else 0 end as 'Octocber',
    Case CalendarMonth when 11 then @Margin else 0 end as 'November',
    Case CalendarMonth when 12 then @Margin else 0 end as 'December'

    From SalesAnalysis2

    where CustomerID = 2829 and BranchID = 0 and CalendarYear = Year(Getdate())

The code above gives this result but only 0s

这是我所做的销售,我需要使用相同的格式来显示保证金。 但是,同一个表中没有边距字段,需要进行计算 Sales Example

3 个答案:

答案 0 :(得分:0)

请尝试:

Declare @Sales numeric, @Cost numeric, @Margin Numeric

Set @Sales = (Select SUM(TotalSales)
        From SalesAnalysis2)

Set @Cost = (Select SUM(TotalCost)
        From SalesAnalysis2)

Set @Margin = ((@Sales - @Cost) / @Sales)

Select distinct

Case Month(YourDateColumn) =1 then @Margin else 0 end as 'January',
Case Month(YourDateColumn) =2 then @Margin else 0 end as 'Febuary',
Case Month(YourDateColumn) =3 then @Margin else 0 end as 'March',
Case Month(YourDateColumn) =4 then @Margin else 0 end as 'April',
Case Month(YourDateColumn) =5 then @Margin else 0 end as 'May',
Case Month(YourDateColumn) =6 then @Margin else 0 end as 'June',
Case Month(YourDateColumn) =7 then @Margin else 0 end as 'July',
Case Month(YourDateColumn) =8 then @Margin else 0 end as 'August',
Case Month(YourDateColumn) =9 then @Margin else 0 end as 'September',
Case Month(YourDateColumn) =10 then @Margin else 0 end as 'Octocber',
Case Month(YourDateColumn) =11 then @Margin else 0 end as 'November',
Case Month(YourDateColumn) =12 then @Margin else 0 end as 'December'

From SalesAnalysis2

where CustomerID = 2829 and BranchID = 0 and CalendarYear = Year(Getdate())

答案 1 :(得分:0)

您需要sum()

Select sum(Case CalendarMonth when 1 then @Margin else 0 end) as January,
       sum(Case CalendarMonth when 2 then @Margin else 0 end) as February,
       . . .
From SalesAnalysis2
where CustomerID = 2829 and BranchID = 0 and
      CalendarYear = Year(Getdate())

答案 2 :(得分:0)

您需要为每个月执行保证金计算,而不是使用您只设置一次的变量。此外,要将case语句用作数据透视表,您还需要MAX聚合来减少返回的行数。否则,您每个月都会得到一行,每行只有一列的值不是0。

WITH p AS
  (
    SELECT      CalendarMonth,
                TotalSales = SUM( TotalSales ),
                TotalCost = SUM( TotalCost ),
                Profit = SUM( TotalSales - TotalCost )
      FROM      SalesAnalysis2
      GROUP BY  CalendarMonth
  )
  SELECT
    MAX(CASE CalendarMonth when 1 then p.Profit/p.TotalSales else 0 END) as 'January',
    MAX(Case CalendarMonth when 2 then p.Profit/p.TotalSales else 0 END) as 'Febuary',
    MAX(Case CalendarMonth when 3 then p.Profit/p.TotalSales else 0 END) as 'March',
    MAX(Case CalendarMonth when 4 then p.Profit/p.TotalSales else 0 END) as 'April',
    MAX(Case CalendarMonth when 5 then p.Profit/p.TotalSales else 0 END) as 'May',
    MAX(Case CalendarMonth when 6 then p.Profit/p.TotalSales else 0 END) as 'June',
    MAX(Case CalendarMonth when 7 then p.Profit/p.TotalSales else 0 END) as 'July',
    MAX(Case CalendarMonth when 8 then p.Profit/p.TotalSales else 0 END) as 'August',
    MAX(Case CalendarMonth when 9 then p.Profit/p.TotalSales else 0 END) as 'September',
    MAX(Case CalendarMonth when 10 then p.Profit/p.TotalSales else 0 END) as 'Octocber',
    MAX(Case CalendarMonth when 11 then p.Profit/p.TotalSales else 0 END) as 'November',
    MAX(Case CalendarMonth when 12 then p.Profit/p.TotalSales else 0 END) as 'December'
  FROM    p

由于您尚未提供任何架构或数据,因此需要对其进行修改以适合您的数据。它可能无法为您的数据提供准确的结果,但它应至少每月返回不同的值,并希望能让您更接近所需的解决方案。