我正在尝试运行一个查询,该查询将告诉我在同一月份输入并具有相同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查询,该查询将为我提供每个项目的总金额。任何帮助将不胜感激。
编辑:
我添加了图像以显示上下文,但其中仅包含阿拉伯语单词:
答案 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