我有一些数据库table.one表调用GL并且有gl_id,gl_type_id等列。 另一个是gl_tran表,有gl_id,trn_amount等。我想得到tran_amount的gl_type_id的摘要。我为此写了一个查询。
SELECT GL.CHAT_ACC_ID,GL.CHAT_ACC_NAME,GL.GL_TYPE_ID,GL.GL_TYPE_NAME,
SUM(CASE WHEN tr.CR_DR = 'CR' AND TR.BRANCH_CODE=1000 AND TR.TRAN_DATE<'2000-01-01' THEN TR.GL_TRN_AMT ELSE 0
END) AS CR_BEFORE,
SUM(CASE WHEN tr.CR_DR='DR' AND TR.BRANCH_CODE=1000 AND TR.TRAN_DATE<'2000-01-01' THEN TR.GL_TRN_AMT ELSE 0
END) AS DR_BEFORE,
SUM(CASE WHEN tr.CR_DR = 'CR' AND TR.BRANCH_CODE=1000 AND TR.TRAN_DATE BETWEEN '2000-01-01' AND '2010-01-01' THEN TR.GL_TRN_AMT ELSE 0
END) AS CR_BETWEEN,
SUM(CASE WHEN tr.CR_DR = 'DR' AND TR.BRANCH_CODE=1000 AND TR.TRAN_DATE BETWEEN '2000-01-01' AND '2010-01-01' THEN TR.GL_TRN_AMT ELSE 0
END) AS DR_BETWEEN
FROM [COREBANKER1].[dbo].[GL_MAIN] as GL
LEFT JOIN [COREBANKER1].[dbo].[GL_DAILY_TRN] AS TR ON GL.GL_ID = TR.GL_CODE
GROUP BY GL.CHAT_ACC_ID,GL.CHAT_ACC_NAME,GL.GL_TYPE_ID,GL.GL_TYPE_NAME
ORDER BY GL.GL_ID
但是这个结果只有在特定gl的事务发生时才会发生。我想要所有GL.CHAT_ACC_ID的摘要。我应该如何更改我对该结果的查询?
答案 0 :(得分:0)
我认为您只需要在select ...
中添加以下语句Sum(TR.GL_TRN_AMT) over() as TotalSum
答案 1 :(得分:0)
我找到了解决方案......:D
SELECT CG.CHART_ACC_ID,CG.CHART_ACC_NAME,CG.GL_TYPE_ID,CG.GL_TYPE_NAME,
SUM(CASE WHEN tr.CR_DR = 'CR' AND TR.BRANCH_CODE=1000 AND TR.TRAN_DATE<'2000-01-01' AND CG.CHART_ACC_ID=GL.CHAT_ACC_ID AND TR.GL_CODE=GL.GL_ID THEN TR.GL_TRN_AMT ELSE 0
END) AS CR_BEFORE,
SUM(CASE WHEN tr.CR_DR='DR' AND TR.BRANCH_CODE=1000 AND TR.TRAN_DATE<'2000-01-01' AND CG.CHART_ACC_ID=GL.CHAT_ACC_ID AND TR.GL_CODE=GL.GL_ID THEN TR.GL_TRN_AMT ELSE 0
END) AS DR_BEFORE,
SUM(CASE WHEN tr.CR_DR = 'CR' AND TR.BRANCH_CODE=1000 AND TR.TRAN_DATE BETWEEN '2000-01-01' AND '2010-01-01' AND CG.CHART_ACC_ID=GL.CHAT_ACC_ID AND TR.GL_CODE=GL.GL_ID THEN TR.GL_TRN_AMT ELSE 0
END) AS CR_BETWEEN,
SUM(CASE WHEN tr.CR_DR = 'DR' AND TR.BRANCH_CODE=1000 AND TR.TRAN_DATE BETWEEN '2000-01-01' AND '2010-01-01' AND CG.CHART_ACC_ID=GL.CHAT_ACC_ID AND TR.GL_CODE=GL.GL_ID THEN TR.GL_TRN_AMT ELSE 0
END) AS DR_BETWEEN
FROM[COREBANKER1].[dbo].[GL_DAILY_TRN] AS TR, [COREBANKER1].[dbo].[GL_MAIN] as GL full JOIN [COREBANKER1].[dbo].GL_CHART_ACC AS CG
ON GL.CHAT_ACC_ID=CG.CHART_ACC_ID
GROUP BY CG.CHART_ACC_ID,CG.CHART_ACC_NAME,CG.GL_TYPE_ID,CG.GL_TYPE_NAME
ORDER BY CG.CHART_ACC_ID