在SQL Server 2000中编写一个特殊的查询

时间:2017-08-13 10:50:24

标签: sql-server-2000

我的数据库是SQL Server 2000,不能使用sum ... over ...

这是我的代码

select 
    cu_account,na_debtamount 
from 
    acc.acc_voucherheader vh 
inner join 
    acc.acc_voucherdetail vd on vh.si_voucherheader = vd.si_voucherheader 
inner join 
    acc.acc_codebook cb on cb.si_account = vd.si_account
where 
    cb.si_account in (1700052, 1700053)   

结果是这样的:

cu_account  na_debtamount   
--------------------------    
60060322       400  
60060322       400  
60060302     25897  
60060310     60917  
60060310     61195  
60060310     64404  
60060310     64404  
60060310     71225  
60060310     84839  
60060306    100000  
60060306    100000  
60060306    100000  
60060306    100000  
60060306    100000  
60060306    100000  
60060306    100000  
60060306    100000  

我想更改我的代码,显示每个na_debtamount cu_account的总和,如下所示:

cu_account     na_debtamount
-------------------------------    
60060322       sum (all the columns that have same cu_account = 60060322)
60060306       sum (all the columns that have same cu_account = 60060306)
and....

谢谢

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您只需按帐户分组并使用常规金额。

select 
    cu_account,sum(na_debtamount) AS na_debtamount
from 
    acc.acc_voucherheader vh 
    inner join acc.acc_voucherdetail vd on 
        vh.si_voucherheader= vd.si_voucherheader 
    inner join acc.acc_codebook cb on 
        cb.si_account =vd.si_account
where 
    cb.si_account in (1700052,1700053)   
group by
    cu_account