我为报告编写了以下sql查询。
Select AT.OTHER_GL_CODE As ACC,
GL.GL_ID,
GL.GL_NAME,
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHCR',
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHDR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERCR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERDR',
AT.OTHER_BRN_CODE BRNCODE,
(
Select Sum( Case When TR.CR_DR = 'DR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As DRBRFORE,
(
Select Sum( Case When TR.CR_DR = 'CR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As CRBRFORE
From COREBANKER1.dbo.MAIN_OTHER_TRN As AT,
COREBANKER1.dbo.GL_MAIN As GL
Where AT.OTHER_BRN_CODE = 1000
And GL.GL_ID = AT.OTHER_GL_CODE
And AT.OTHER_BANK_DATE
Between '2017-07-01' And '2017-07-30';
这是输出
但是我想要的输出就在这里
那么我如何更改我的查询以获得所需的结果(分组到ACC和GL_ID)?
答案 0 :(得分:0)
快速而肮脏的方法是将整个查询和分组包装起来,这样可以防止3,4,5,6列中的重复值。
更干净的方法是获取这4列并将每个列包装在Select SUM(...)
中,就像您为后面的列所做的那样。
简而言之,没有GROUP BY
会给你留下重复的内容 - 我冒昧地为你提供快速而肮脏的方法的语法,见下文。
SELECT
ACC
,GL_ID
,GL_NAME
,SUM(CASHCR) AS CASHCR
,SUM(CASHDR) AS CASHDR
,SUM(OTHERCR) AS OTHERCR
,SUM(OTHERDR) AS OTHERDR
,BRNCODE
,DRBRFORE
,CRBRFORE
FROM (
Select AT.OTHER_GL_CODE As ACC,
GL.GL_ID,
GL.GL_NAME,
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHCR',
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHDR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERCR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERDR',
AT.OTHER_BRN_CODE BRNCODE,
(
Select Sum( Case When TR.CR_DR = 'DR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As DRBRFORE,
(
Select Sum( Case When TR.CR_DR = 'CR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As CRBRFORE
From COREBANKER1.dbo.MAIN_OTHER_TRN As AT,
COREBANKER1.dbo.GL_MAIN As GL
Where AT.OTHER_BRN_CODE = 1000
And GL.GL_ID = AT.OTHER_GL_CODE
And AT.OTHER_BANK_DATE
Between '2017-07-01' And '2017-07-30'
)A
GROUP BY
ACC
,GL_ID
,GL_NAME
,CASHCR
,CASHDR
,OTHERCR
,OTHERDR
,BRNCODE
,DRBRFORE
,CRBRFORE
;
答案 1 :(得分:0)
您可以使用CTE包围查询,然后汇总所需的字段。
WITH this as
(
Select AT.OTHER_GL_CODE As ACC,
GL.GL_ID,
GL.GL_NAME,
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHCR',
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHDR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERCR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERDR',
AT.OTHER_BRN_CODE BRNCODE,
(
Select Sum( Case When TR.CR_DR = 'DR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As DRBRFORE,
(
Select Sum( Case When TR.CR_DR = 'CR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As CRBRFORE
From COREBANKER1.dbo.MAIN_OTHER_TRN As AT,
COREBANKER1.dbo.GL_MAIN As GL
Where AT.OTHER_BRN_CODE = 1000
And GL.GL_ID = AT.OTHER_GL_CODE
And AT.OTHER_BANK_DATE
Between '2017-07-01' And '2017-07-30';
)
)
SELECT ACC ,GL_ID ,GL_NAME, SUM(CASHCR) AS CASHCR ,SUM(CASHDR) AS CASHDR ,SUM(OTHERCR) AS OTHERCR ,SUM(OTHERDR) AS OTHERDR, BRNCODE, DRBRFORE, CRBRFORE
FROM this
GROUP BY ACC ,GL_ID ,GL_NAME /*,CASHCR ,CASHDR ,OTHERCR ,OTHERDR */,BRNCODE
,DRBRFORE ,CRBRFORE