gpt4 book ai didi

mysql - 想计算group by option渲染的count的总和

转载 作者:行者123 更新时间:2023-11-29 07:14:11 27 4
gpt4 key购买 nike

我有一个表,其中包含 id、tid、companyid、ttype 等列。

许多公司ID的ID可能相同,但在公司ID内是唯一的,并且tid 始终是唯一的,并且

我想计算表格中输入的交易总数,

单个事务可以插入多行,

例如,

id tid companyid ttype 
1 1 1 xxx
1 2 1 may be null
2 3 1 yyy
2 4 1 may be null
2 5 1 may be null

以上条目应仅计为 2 笔交易..

它可能会重复许多 companyids..

那么我如何计算表格中输入的交易总数

我试过了

select sum(count(*)) from transaction group by id,companyId; 

但是没用

select count(*) from transaction group by id; 

不会工作,因为不同的公司 ID 可能会重复 ID。

最佳答案

试试这个

SELECT SUM(s.counts) transactions FROM (SELECT COUNT(*) counts FROM transaction GROUP BY id,companyId) s; 

关于mysql - 想计算group by option渲染的count的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2590498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com