sql2008如何实现查询同一字段不同的数据各自总和

时间:2021-12-26 15:13:20
create table customs
(
  cus_id int identity(1,1) primary key,
  cus_name nvarchar(20),
  cus_status nvarchar(10) default '未确认'  /*未确认;确认;签约;已放弃*/
)
create view _customs
as 
select count(cus_status) as status_Y  from customs
where cus_status='签约' 
现在我只是实现了查询同一字段不某个数据的总和,但我想实现的是查询同一字段不同的数据各自总和 sql2008如何实现查询同一字段不同的数据各自总和

4 个解决方案

#1


select
     sum(case when cus_status='未确认'  then 1 else 0 end) as 未确认' ,
     sum(case when cus_status='确认'  then 1 else 0 end) as '确认' ,
     sum(case when cus_status='签约'  then 1 else 0 end) as 签约' ,
     sum(case when cus_status='已放弃'  then 1 else 0 end) as 已放弃' 
from
      customs

#2


select
     sum(case when cus_status='未确认'  then 1 else 0 end) as 未确认' ,
     sum(case when cus_status='确认'  then 1 else 0 end) as '确认' ,
     sum(case when cus_status='签约'  then 1 else 0 end) as '签约' ,
     sum(case when cus_status='已放弃'  then 1 else 0 end) as 已放弃' 
from
      customs

#3


select
     sum(case when cus_status='未确认'  then 1 else 0 end) as 未确认' ,
     sum(case when cus_status='确认'  then 1 else 0 end) as '确认' ,
     sum(case when cus_status='签约'  then 1 else 0 end) as '签约' ,
     sum(case when cus_status='已放弃'  then 1 else 0 end) as '已放弃' 
from
      customs

#4


sql2008如何实现查询同一字段不同的数据各自总和sql2008如何实现查询同一字段不同的数据各自总和

#1


select
     sum(case when cus_status='未确认'  then 1 else 0 end) as 未确认' ,
     sum(case when cus_status='确认'  then 1 else 0 end) as '确认' ,
     sum(case when cus_status='签约'  then 1 else 0 end) as 签约' ,
     sum(case when cus_status='已放弃'  then 1 else 0 end) as 已放弃' 
from
      customs

#2


select
     sum(case when cus_status='未确认'  then 1 else 0 end) as 未确认' ,
     sum(case when cus_status='确认'  then 1 else 0 end) as '确认' ,
     sum(case when cus_status='签约'  then 1 else 0 end) as '签约' ,
     sum(case when cus_status='已放弃'  then 1 else 0 end) as 已放弃' 
from
      customs

#3


select
     sum(case when cus_status='未确认'  then 1 else 0 end) as 未确认' ,
     sum(case when cus_status='确认'  then 1 else 0 end) as '确认' ,
     sum(case when cus_status='签约'  then 1 else 0 end) as '签约' ,
     sum(case when cus_status='已放弃'  then 1 else 0 end) as '已放弃' 
from
      customs

#4


sql2008如何实现查询同一字段不同的数据各自总和sql2008如何实现查询同一字段不同的数据各自总和