按帐户代码分组

时间:2018-01-04 17:37:28

标签: sql-server sql-server-2008 sql-server-2005

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子句中。

1 个答案:

答案 0 :(得分:0)

你想要那样的东西;

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

Code次计算,并应用All Companies

的where子句