create table tb(id int ,class varchar)--class种类就只有三种,如果不固定就需要存储过程来实现
insert tb 
select 1,'a' union all
select 1,'a' union all
select 1,'b' union all
select 1,'c' union all
select 2,'a' union all
select 2,'b' union all
select 2,'b' 
select * from tb

--想查找出按id分组得到的 a  ,b  ,c 的数量
--  如下
--id   a   b    c
--1   2   1     1
--2   1   2    0 


select 
 id,
 
 a=sum(case class when 'a' then 1 else 0 end),
 b=sum(case class when 'b' then 1 else 0 end),
 c=sum(case class when 'c' then 1 else 0 end)
from 
 tb
group by
 id

 

相关文章:

  • 2021-07-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2022-12-23
  • 2022-12-23
  • 2021-12-30
猜你喜欢
  • 2022-12-23
  • 2021-08-20
  • 2021-08-09
  • 2021-11-27
  • 2021-12-05
  • 2022-12-23
相关资源
相似解决方案