34,587
社区成员
发帖
与我相关
我的任务
分享
create table tb(err_date datetime,err_type varchar(10))
insert into tb values('2010-06-06' ,'err_type_1')
insert into tb values('2010-06-06' ,'err_type_4')
insert into tb values('2010-06-06' ,'err_type_2')
insert into tb values('2010-06-06' ,'err_type_3')
insert into tb values('2010-06-07' ,'err_type_3')
insert into tb values('2010-06-07' ,'err_type_1')
insert into tb values('2010-06-07' ,'err_type_4')
insert into tb values('2010-06-07' ,'err_type_2')
insert into tb values('2010-06-07' ,'err_type_5')
insert into tb values('2010-06-07' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_1')
insert into tb values('2010-06-08' ,'err_type_5')
insert into tb values('2010-06-08' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_2')
insert into tb values('2010-06-08' ,'err_type_4')
insert into tb values('2010-06-08' ,'err_type_4')
go
select
sum(case err_type when 'err_type_1' then 1 else 0 end) as count1,
sum(case err_type when 'err_type_2' then 1 else 0 end) as count2,
sum(case err_type when 'err_type_3' then 1 else 0 end) as count3,
sum(case err_type when 'err_type_4' then 1 else 0 end) as count4,
sum(case err_type when 'err_type_5' then 1 else 0 end) as count5,
'总计' = count(1)
from tb t where err_date='2010-06-06'
group by err_date
/*
count1 count2 count3 count4 count5 总计
----------- ----------- ----------- ----------- ----------- -----------
1 1 1 1 0 4
(所影响的行数为 1 行)
*/
drop table tb
create table tb(err_date datetime,err_type varchar(10))
insert into tb values('2010-06-06' ,'err_type_1')
insert into tb values('2010-06-06' ,'err_type_4')
insert into tb values('2010-06-06' ,'err_type_2')
insert into tb values('2010-06-06' ,'err_type_3')
insert into tb values('2010-06-07' ,'err_type_3')
insert into tb values('2010-06-07' ,'err_type_1')
insert into tb values('2010-06-07' ,'err_type_4')
insert into tb values('2010-06-07' ,'err_type_2')
insert into tb values('2010-06-07' ,'err_type_5')
insert into tb values('2010-06-07' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_1')
insert into tb values('2010-06-08' ,'err_type_5')
insert into tb values('2010-06-08' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_2')
insert into tb values('2010-06-08' ,'err_type_4')
insert into tb values('2010-06-08' ,'err_type_4')
go
select
sum(case err_type when 'err_type_1' then 1 else 0 end) as count1,
sum(case err_type when 'err_type_2' then 1 else 0 end) as count2,
sum(case err_type when 'err_type_3' then 1 else 0 end) as count3,
sum(case err_type when 'err_type_4' then 1 else 0 end) as count4,
sum(case err_type when 'err_type_5' then 1 else 0 end) as count5,
'总计' = (select count(1) from tb where err_date = '2010-06-06')
from tb t where err_date='2010-06-06'
group by err_date
/*
count1 count2 count3 count4 count5 总计
----------- ----------- ----------- ----------- ----------- -----------
1 1 1 1 0 4
(所影响的行数为 1 行)
*/
select
[err_type_1] = (select count(1) from tb where err_date = '2010-06-06' and err_type = 'err_type_1'),
[err_type_2] = (select count(1) from tb where err_date = '2010-06-06' and err_type = 'err_type_2'),
[err_type_3] = (select count(1) from tb where err_date = '2010-06-06' and err_type = 'err_type_3'),
[err_type_4] = (select count(1) from tb where err_date = '2010-06-06' and err_type = 'err_type_4'),
[err_type_5] = (select count(1) from tb where err_date = '2010-06-06' and err_type = 'err_type_5'),
[总计] = (select count(1) from tb where err_date = '2010-06-06' )
/*
err_type_1 err_type_2 err_type_3 err_type_4 err_type_5 总计
----------- ----------- ----------- ----------- ----------- -----------
1 1 1 1 0 4
(所影响的行数为 1 行)
*/
drop table tb
create table tb(err_date datetime,err_type varchar(10))
insert into tb values('2010-06-06' ,'err_type_1')
insert into tb values('2010-06-06' ,'err_type_4')
insert into tb values('2010-06-06' ,'err_type_2')
insert into tb values('2010-06-06' ,'err_type_3')
insert into tb values('2010-06-07' ,'err_type_3')
insert into tb values('2010-06-07' ,'err_type_1')
insert into tb values('2010-06-07' ,'err_type_4')
insert into tb values('2010-06-07' ,'err_type_2')
insert into tb values('2010-06-07' ,'err_type_5')
insert into tb values('2010-06-07' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_1')
insert into tb values('2010-06-08' ,'err_type_5')
insert into tb values('2010-06-08' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_2')
insert into tb values('2010-06-08' ,'err_type_4')
insert into tb values('2010-06-08' ,'err_type_4')
go
select
[err_type_1] = (select count(1) from tb where err_date = '2010-06-06' and err_type = 'err_type_1'),
[err_type_2] = (select count(1) from tb where err_date = '2010-06-06' and err_type = 'err_type_2'),
[err_type_3] = (select count(1) from tb where err_date = '2010-06-06' and err_type = 'err_type_3'),
[err_type_4] = (select count(1) from tb where err_date = '2010-06-06' and err_type = 'err_type_4'),
[err_type_5] = (select count(1) from tb where err_date = '2010-06-06' and err_type = 'err_type_5'),
[总计] = (select count(1) from tb where err_date = '2010-06-06' )
/*
err_type_1 err_type_2 err_type_3 err_type_4 err_type_5 总计
----------- ----------- ----------- ----------- ----------- -----------
1 1 1 1 0 4
(所影响的行数为 1 行)
*/
drop table tb
declare @date varchar(10)
set @date='2010-06-08'
select 'err_type_1'=sum( case err_type when'err_type_1'then 1 else 0 end),
'err_type_2'=sum( case err_type when'err_type_2'then 1 else 0 end),
'err_type_3'=sum( case err_type when'err_type_3'then 1 else 0 end),
'err_type_4'=sum( case err_type when'err_type_4'then 1 else 0 end),
'err_type_5'=sum( case err_type when'err_type_5'then 1 else 0 end) ,
sum( case err_type when'err_type_1'then 1 else 0 end)+sum( case err_type when'err_type_2'then 1 else 0 end)
+sum( case err_type when'err_type_3'then 1 else 0 end)+sum( case err_type when'err_type_4'then 1 else 0 end)
+sum( case err_type when'err_type_5'then 1 else 0 end) as 总计
from tb
where err_date=@date
group by err_date
err_type_1 err_type_2 err_type_3 err_type_4 err_type_5 总计
1 1 2 2 1 7
create table tb(err_date datetime,err_type varchar(10))
insert into tb values('2010-06-06' ,'err_type_1')
insert into tb values('2010-06-06' ,'err_type_4')
insert into tb values('2010-06-06' ,'err_type_2')
insert into tb values('2010-06-06' ,'err_type_3')
insert into tb values('2010-06-07' ,'err_type_3')
insert into tb values('2010-06-07' ,'err_type_1')
insert into tb values('2010-06-07' ,'err_type_4')
insert into tb values('2010-06-07' ,'err_type_2')
insert into tb values('2010-06-07' ,'err_type_5')
insert into tb values('2010-06-07' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_1')
insert into tb values('2010-06-08' ,'err_type_5')
insert into tb values('2010-06-08' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_3')
insert into tb values('2010-06-08' ,'err_type_2')
insert into tb values('2010-06-08' ,'err_type_4')
insert into tb values('2010-06-08' ,'err_type_4')
go
select
err_type,
sum(case err_type when 'err_type_1' then 1 else 0 end) as count1,
sum(case err_type when 'err_type_2' then 1 else 0 end) as count2,
sum(case err_type when 'err_type_3' then 1 else 0 end) as count3,
sum(case err_type when 'err_type_4' then 1 else 0 end) as count4,
sum(case err_type when 'err_type_5' then 1 else 0 end) as count5,
'总计' = (select count(1) from tb where err_date = '2010-06-06')
from tb t where err_date='2010-06-06'
group by err_type
drop table tb
/*
err_type count1 count2 count3 count4 count5 总计
---------- ----------- ----------- ----------- ----------- ----------- -----------
err_type_1 1 0 0 0 0 4
err_type_2 0 1 0 0 0 4
err_type_3 0 0 1 0 0 4
err_type_4 0 0 0 1 0 4
(所影响的行数为 4 行)
*/