如何为SQL Server中具有相同accname帐户的行求和

时间:2019-05-06 13:59:24

标签: c# asp.net tsql

我正在尝试运行一个查询,该查询将告诉我在同一月份输入并具有相同mainacc,subacc的每个相同项目的总和(贷项)。我对同一项目有多行并且没有总和

的原因
SELECT MovingBudget.mainacc, MovingBudget.subacc, MovingBudget.accname, MovingBudget.movingbudgetmony, ChequeRecord.credit,ChequeRecord.docdate,                        DATEDIFF(MM,ChequeRecord.docdate, GETDATE()),    DATEDIFF(YY,ChequeRecord.docdate, GETDATE()),
    SUM(CASE WHEN DATEDIFF(MM, ChequeRecord.docdate, GETDATE()) =0 THEN ChequeRecord.credit END)  AS monthmove, 
      SUM(CASE WHEN DATEDIFF(MM, ChequeRecord.docdate, GETDATE()) >= 1 THEN ChequeRecord.credit END) AS modawer,
    SUM(CASE WHEN DATEDIFF(yy, ChequeRecord.docdate, GETDATE()) = 0 THEN ChequeRecord.credit END) AS summation

               FROM  
                ChequeRecord INNER JOIN
                MovingBudget 
                ON ChequeRecord.accname = MovingBudget.accname
    WHERE 
    (SUBSTRING(MovingBudget.mainacc, 1, 1) = '1') 
    AND (SUBSTRING(MovingBudget.subacc, 1, 1) = '1') 

    GROUP BY MovingBudget.mainacc, MovingBudget.subacc, MovingBudget.accname, MovingBudget.movingbudgetmony, ChequeRecord.credit,ChequeRecord.docdate;

我无法提出SELECT查询,该查询将为我提供每个项目的总金额。任何帮助将不胜感激。

编辑:

我添加了图像以显示上下文,但其中仅包含阿拉伯语单词:

enter image description here

1 个答案:

答案 0 :(得分:0)

对于基本分组应该是这样。

select mainacc, subacc, datepart(month,docdate), sum(credit) from myTable group by mainacc, subacc, datepart(month,docdate)

但是我有一种感觉,您正在寻找运行值计算。在这种情况下,请检查此主题How to get running sum of a column in sql server