SQL语句中,如何根据条件分组
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)
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)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询