我在下面有这两个查询,用于计算从 1 月到 12 月的净利润。
查询 1
/* Profit for the Period 13 */
SELECT * FROM
(
SELECT MONTH(T0.RefDate) AS Month, '311999' AS 'SAP Code', 'Period Profit' AS 'Description', SUM (T0.[Credit] - T0.[Debit]) AS 'Amount',
'311200' AS 'FatherNum', 12 AS 'Group', 'Liabilities' [Liabilities]
FROM JDT1 T0 WHERE (T0.RefDate BETWEEN DATEADD(yy, DATEDIFF(yy,0,'20151231'), 0) AND '20151231')
AND T0.[ContraAct] <> '311201'
GROUP BY T0.RefDate) AS q
PIVOT
(
SUM(Amount)
FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS query
结果:
查询 2
/* Profit for the Period 13 */
SELECT * FROM
(
SELECT MONTH(T1.RefDate) AS Month, '311999' AS 'SAP Code', 'Period Profit' AS 'Description', SUM (T1.[Credit] - T1.[Debit]) AS 'Amount',
'311200' AS 'FatherNum', 12 AS 'Group', 'Liabilities' [Liabilities]
FROM OACT T0 INNER JOIN JDT1 T1 ON T0.[AcctCode] = T1.[Account]
WHERE (T1.RefDate BETWEEN DATEADD(yy, DATEDIFF(yy,0,'20151231'), 0) AND '20151231')
AND T0.GroupMask > 3
GROUP BY T1.RefDate) AS q
PIVOT
(
SUM(Amount)
FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) AS query
结果:
两者之间的差异出现在第 12 个月。查询 1 准确地获得了 Dec 利润,因为它是一个在 12 月关闭 2015 年的过程后存储数据的帐户。
如何将两者合并为一个查询,以便获得查询 2 的所有结果,第 12 个月除外,而不是 -4764758.550000 我得到 3280335.630000?
我创建了 2 个表变量以尝试匹配您的环境。我已将 UNION ALL 添加到您的原始查询中,一个查询选择除 12 月以外的所有其他月份,第二个查询仅选择 12 月。
还有其他方法可以做到这一点,但无论如何,它看起来都是一个昂贵的查询。
开始了:
结果,你可以比较十月和十二月。