帮助了解 SQL 总计和总和

发布于 2024-10-19 06:21:20 字数 633 浏览 9 评论 0原文

我有这个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  

我只想显示四列帐户、日期、计数和总和

帐户将为 a.acct_no。
该日期将是该帐户的唯一 m.cmcl_forecast_cleared 日期
计数将是这些唯一日期 (cmcl_forecast_cleared) 的检查(记录)总数
总和将是这些唯一日期的总 check_amount (cmcl_forecast_cleared)

我希望这是可以理解的。我不确定是否需要任何分组

I have this SQL. Im not sure it is what i need, but it does select all the records i need. I just need to condense them now and total the counts and amounts for unique dates.

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  

I only want to display four columns Account, Date, Count, and sum

The account would be a.acct_no.
The date would all unique m.cmcl_forecast_cleared date for that account
The count would be total no of checks (records) for those unique dates (cmcl_forecast_cleared)
The sum would be the total check_amount for thos unique dates (cmcl_forecast_cleared)

I hope this is understandable. Im not sure if i need any grouping or not

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2024-10-26 06:21:20
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

您可以根据需要按任意数量的列进行分组。

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

You group by as many columns as you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文