我有这个SQL。我不确定它是我需要的,但它确实选择了我需要的所有记录。我现在只需要压缩它们并计算唯一日期的计数和金额。
select m.account_tag, m.cmcl_forecast_cleared, m.check_amount,
a.acct_id, a.acct_no, a.bank_id,
b.bank_id, b.name
from ap_master m
join accounts a on a.acct_id=m.account_tag
join banks b on b.bank_id=a.bank_id
where m.cmcl_bank_cleared is null
order by m.account_tag, m.cmcl_forecast_cleared
我只想显示四列Account,Date,Count和sum
该帐户将是a.acct_no 该日期将是该帐户的唯一m.cmcl_forecast_cleared日期 计数将是这些唯一日期(cmcl_forecast_cleared)的检查(记录)的总数 总和将是thos唯一日期的总check_amount(cmcl_forecast_cleared)
我希望这是可以理解的。我不确定我是否需要任何分组
答案 0 :(得分:5)
select
a.acct_no, m.cmcl_forecast_cleared, b.name,
count(*) as TotalChecks,
Sum(m.check_amount) as TotalAmount
from ap_master m
join accounts a on a.acct_id=m.account_tag
join banks b on b.bank_id=a.bank_id
where m.cmcl_bank_cleared is null
group by a.acct_no, m.cmcl_forecast_cleared, b.name
order by a.acct_no, m.cmcl_forecast_cleared, b.name
您可以根据需要分组尽可能多的列。