求SQL语句根据字段分组

2025-03-04 02:01:11
推荐回答(3个)
回答1:

首先,使用下面语句:
select date, count(date) cnt from user where date is not null group by date
结果:
date cnt
1月 2
2月 2
3月 1

在上面语句基础上改进,使之与要求结果一致:
select max(case when date = '1月' then cnt else 0 end ) 1月,
max(case when date = '2月' then cnt else 0 end ) 2月,
max(case when date = '3月' then cnt else 0 end ) 3月
from (select date, count(date) cnt from user where date is not null group by date)

回答2:

select date,count(date) from user where date is not null group by date;

回答3:

select date,coun(date) from user where date is not null group by date