在 SQL Server 2000 中编写特殊查询

Write a special query in SQL Server 2000

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

这是我的代码

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  

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

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....

谢谢

如果我没理解错的话,您可以简单地按帐户分组并使用常规金额。

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