我对一个非常大的数据库中的count方法有疑问。 以下哪一项更有效?是否有其他可能性来获得相同的结果?感谢您的想法和回复。
方法1:
select
sum(Case when customertype = 'BANK' then 1 else 0 end) as Cust_is_Bank,
sum(Case when customertype = 'BROKER' then 1 else 0 end) as Cust_is_Broker,
sum(Case when customertype not in ('BANK','BROKER') then 1 else 0 end) as Cust_is_Non
from bondtrades
where 1=1;
方法2:
select
customertype,
count(customertype)
from bondtrades
group by customertype;
方法1的结果:
Cust_is_Bank Cust_is_Broker Cust_is_Non
273 64 8
方法2的其余部分:
Customertype Count(Coustomertype)
BROKER 64
BANK 273
OTHER 8