gpt4 book ai didi

sql-server-2012 - SQL - 返回累积每月列值

转载 作者:行者123 更新时间:2023-12-04 06:55:44 27 4
gpt4 key购买 nike

我有这个 SQL 查询,用于查找每个帐户每月的试算表变动。

/* Fixed Assets 1 */
SELECT * FROM
(
SELECT T0.AcctCode AS 'SAP Code', T0.AcctName AS 'Description',
MONTH(T1.RefDate) AS Month, SUM(T1.Debit - T1.Credit) AS 'Amount'
FROM OACT T0
LEFT JOIN JDT1 T1 ON T0.[AcctCode] = T1.[Account]
WHERE T0.AcctCode LIKE '111%' AND T0.Levels = 5
AND (T1.RefDate BETWEEN DATEADD(yy, DATEDIFF(yy,0,{?AsAtDate}), 0) AND {?AsAtDate})
GROUP BY T0.AcctCode, T0.AcctName, T0.FatherNum, T1.RefDate) AS q
PIVOT
(
SUM(Amount)
FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS query

这将返回六月的以下结果:

enter image description here

我想包括所有行的累积每月结果,这样行
1 结果应该是:
1, 111110, Building Gross Value, 633604.23, 637764.23, 645313.03, 649061.78, 651097.78, 651097.78, 651397.78

我没有成功实现这一目标。如何修改查询以实现此目的?

附加数据:

查询:
SELECT CAST(T0.TaxDate AS Date), T0.Account, T1.AcctName, T0.Debit, T0.Credit
FROM JDT1 T0 INNER JOIN OACT T1
ON T0.Account = T1.AcctCode
WHERE T0.[Account] = '111110'

返回:

enter image description here

最佳答案

您需要外部选择,您可以在其中将列添加到另一个:

SELECT AcctCode AS 'SAP Code', AcctName AS 'Description', 
col1 AS '1',
col1 + col2 AS '2',
col1 + col2 + col3 AS '3',
... FROM (
SELECT AcctCode, AcctName,
[1] AS col1,
[2] AS col2,
[3] AS col3,
... FROM
(
SELECT T0.AcctCode, T0.AcctName,
MONTH(T1.RefDate) AS Month, SUM(T1.Debit - T1.Credit) AS 'Amount'
FROM OACT T0
LEFT JOIN JDT1 T1 ON T0.[AcctCode] = T1.[Account]
WHERE T0.AcctCode LIKE '111%' AND T0.Levels = 5
AND (T1.RefDate BETWEEN DATEADD(yy, DATEDIFF(yy,0,{?AsAtDate}), 0) AND {?AsAtDate})
GROUP BY T0.AcctCode, T0.AcctName, T0.FatherNum, T1.RefDate) AS q
PIVOT
(
SUM(Amount)
FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS query
)

关于sql-server-2012 - SQL - 返回累积每月列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36083018/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com