按帐户代码分组

问题描述 投票:0回答:1

enter image description here

我想要信用,借方和余额的结果总和代码明智,但我担心如何使用所有公司部门的帐户代码组

Select 'All Companies' as DivisionNameEn,
        a.LF_CompanyDivisionID,
        a.AccountCode as code,
        a.AccountNameEn as name,
        sum(ISNULL(Credit,0) - ISNULL(Debit,0)) as balance,
        sum(ISNULL(Credit,0)) as credit,
        sum(ISNULL(debit,0)) as debit 
        from View_ChartOfAccount a 

                                group by a.AccountCode 

它给了我错误

列'View_ChartOfAccount.LF_CompanyDivisionID'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

sql-server sql-server-2008 sql-server-2005
1个回答
0
投票

你想要那样的东西吗?

select Code, sum(balance), sum(credit), sum(debit) from 
table 
where DivisionNameEn = 'All Companies'
group by Code

sum计算每Code并适用于All Companies的where子句

© www.soinside.com 2019 - 2024. All rights reserved.