总和和部分总和

时间:2017-07-25 13:28:33

标签: sql sql-server

我目前正在使用SSMS。我正在提取数据,并尝试获得两个不同的列,总结价格。两列'ChangeSpend'和'TotalSpend'都引用了同一列,这就是我遇到问题的地方。

我希望ChangeSpend返回每个收据的所有代码的总和,以V.Ch%开头(因此它们排除所有其他代码)和TotalSpend,以汇总每张收据的所有代码。

这是我目前的代码:

SELECT 
      Receipt
      ,ReceiptCode
      ,ReceiptAmount
      ,sum(ReceiptAmount) over (Partition by Receipt) as TotalSpend
      ,(CASE WHEN ReceiptCode = 'V.Ch%' then sum(ReceiptAmount) 
                                             over (Partition by Receipt) 
      ELSE 0
      END) as ChangeSpend
  FROM tableA
  LEFT OUTER JOIN tableB
  on A.Receipt = B.Receipt
  WHERE ReceiptCode LIKE 'V.%'
  ORDER BY Receipt

但是,我的查询目前打印出来:

Receipt    ReceiptCode    ReceiptAmount    TotalSpend    ChangeSpend
1          v.cha          5                20            0         
1          v.rt           2                20            0
1          v.chb          6                20            0
1          v.abc          7                20            0
2          v.cha          20               21            0
2          v.abc          1                21            0
3          v.cha          4                14            0
3          v.chb          1                14            0
3          v.tye          7                14            0
3          v.chs          2                14            0

我希望打印出来:

Receipt    ReceiptCode    ReceiptAmount    TotalSpend    ChangeSpend
1          v.cha          5                20            11         
1          v.rt           2                20            11
1          v.chb          6                20            11
1          v.abc          7                20            11
2          v.cha          20               21            20
2          v.abc          1                21            20
3          v.cha          4                14            7
3          v.chb          1                14            7
3          v.tye          7                14            7
3          v.chs          2                14            7

感谢您的帮助

4 个答案:

答案 0 :(得分:1)

你必须将SUM放在CASE之外,而不是相反:

SUM(CASE WHEN SomeCondition=true THEN MyColumn ELSE 0 END)

答案 1 :(得分:1)

尝试

    SUM(
CASE WHEN ReceiptCode like 'V.Ch%' then ReceiptAmount ELSE 0 END) as ChangeSpend

答案 2 :(得分:0)

[NSLayoutConstraint
    constraintWithItem: view2    // <-- constrain this view
    attribute:NSLayoutAttributeTop
    relatedBy:NSLayoutRelationEqual
    toItem: view1               //  <-- to this view
    attribute:NSLayoutAttributeTop
    multiplier:1.0
    constant:0.0];

答案 3 :(得分:0)

这可能有所帮助:

    Create Table Payment(
    Receipt Int,
    ReceiptCode VARCHAR(10),
    ReceiptAmount decimal)


    Insert Into Payment
    Values
    (1, 'v.cha', 5),
    (1, 'v.rt', 2),
    (1, 'v.chb', 6),
    (1, 'v.abc', 7),
    (2, 'v.cha', 20),
    (2, 'v.abc', 1),
    (3, 'v.cha', 4),
    (3, 'v.chb', 1),
    (3, 'v.the', 7),
    (3, 'v.chs', 2);

    SELECT  * ,
        SUM(ReceiptAmount) OVER ( PARTITION BY Receipt ) AS TotalSpend ,
        SUM(IIF(ReceiptCode LIKE 'v.ch%',ReceiptAmount,0)) OVER ( PARTITION 
        BY Receipt ) AS ChangeSpend
    FROM    payment;

结果:

enter image description here