当检查号码相同时,我正试图将数据行数加到Sum。
select
chkdetail_t.check_acct,
chkheader_t.acct_number,
chkdetail_t.check_number,
SUM(CASE WHEN chkdetail_t.check_number = chkdetail_t.check_number THEN chkdetail_t.amount ELSE NULL END) AS chek_total,
convert(VARCHAR(10),CAST(chkdetail_t.check_date as datetime), 101) AS chk_date,
chkdetail_t.amount,
chkdetail_t.vend_name,
chkdetail_t.chk_status,
chkdetail_t.reconsiled,
chkdetail_t.chk_type
from dbo.chkdetail_t
join dbo.chkheader_t
on chkheader_t.check_acct = chkdetail_t.check_acct
where chkdetail_t.check_acct = 'HUNTINGTON'
and chkdetail_t.chk_status not in ('V', 'R')
and chkdetail_t.chk_type != 'R'
and chkdetail_t.reconsiled = 0
group by chkdetail_t.check_acct,
chkheader_t.acct_number,
chkdetail_t.check_number,
chkdetail_t.check_date,
chkdetail_t.amount,
chkdetail_t.vend_name,
chkdetail_t.chk_status,
chkdetail_t.reconsiled,
chkdetail_t.chk_type
check_number chek_total
065757 50.00
065757 522.20
065757 581.70
065757 4492.88
065757 4832.92
我需要从上到下扫描check_number行并找到彼此相等的行,这样我就可以计算每个校验码的总和。
答案 0 :(得分:-1)
SELECT T.Check_Number, SUM(T.amount)
FROM chkdetail_t AS D
JOIN cheheader_t AS H
ON H.check_acct = T.check_acct
GROUP BY T.check_Number