表A
分级 类型1 类型2 类型3 类型4 金额 条件
A 是 是 否 是 500 完成
A 是 否 是 是 500 完成
B 是 是 是 是 100 完成
B 是 否 是 是 200 完成
..................................
==========================================================
结果求统计成下面结果,条件等于“完成”。分级 类型1个数(字符为‘是') 类型2个数 类型3个数 类型4个数 金额合计
A 2 1 1 2 1000
B 2 1 2 2 300
分级 类型1 类型2 类型3 类型4 金额 条件
A 是 是 否 是 500 完成
A 是 否 是 是 500 完成
B 是 是 是 是 100 完成
B 是 否 是 是 200 完成
..................................
==========================================================
结果求统计成下面结果,条件等于“完成”。分级 类型1个数(字符为‘是') 类型2个数 类型3个数 类型4个数 金额合计
A 2 1 1 2 1000
B 2 1 2 2 300
分级,
sum(case when 类型1='是' then 1 else 0 end) as 类型1个数,
sum(case when 类型2='是' then 1 else 0 end) as 类型2个数,
sum(case when 类型3='是' then 1 else 0 end) as 类型3个数,
sum(case when 类型4='是' then 1 else 0 end) as 类型4个数,
sum(金额)
from
A
group by
分级
类型1个数=sum(case when 类型1='是' then 1 else 0 end ),
类型2个数=sum(case when 类型2='是' then 1 else 0 end ),
类型3个数=sum(case when 类型3='是' then 1 else 0 end ),
类型4个数=sum(case when 类型4='是' then 1 else 0 end ),
金额=sum(金额)
from ta group by 分级
(select count(1) from a where 类型1 = '是' and 分级 = t.分级 and 条件 = '完成') 类型1个数,
(select count(1) from a where 类型2 = '是' and 分级 = t.分级 and 条件 = '完成') 类型2个数,
(select count(1) from a where 类型3 = '是' and 分级 = t.分级 and 条件 = '完成') 类型3个数,
(select count(1) from a where 类型4 = '是' and 分级 = t.分级 and 条件 = '完成') 类型4个数,
(select sum(金额) from a where 分级 = t.分级) 金额合计
from a t
go
create table [A]([分级] varchar(1),[类型1] varchar(2),[类型2] varchar(2),[类型3] varchar(2),[类型4] varchar(2),[金额] int,[条件] varchar(4))
insert [A]
select 'A','是','是','否','是',500,'完成' union all
select 'A','是','否','是','是',500,'完成' union all
select 'B','是','是','是','是',100,'完成' union all
select 'B','是','否','是','是',200,'完成'select
分级,
sum(case when 类型1='是' then 1 else 0 end) as 类型1个数,
sum(case when 类型2='是' then 1 else 0 end) as 类型2个数,
sum(case when 类型3='是' then 1 else 0 end) as 类型3个数,
sum(case when 类型4='是' then 1 else 0 end) as 类型4个数,
sum(金额)
from
A
group by
分级
--测试结果:
/*
分级 类型1个数 类型2个数 类型3个数 类型4个数
---- ----------- ----------- ----------- ----------- -----------
A 2 1 1 2 1000
B 2 1 2 2 300(2 行受影响)
*/
(select count(1) from a where 类型1 = '是' and 分级 = t.分级 and 条件 = '完成') 类型1个数,
(select count(1) from a where 类型2 = '是' and 分级 = t.分级 and 条件 = '完成') 类型2个数,
(select count(1) from a where 类型3 = '是' and 分级 = t.分级 and 条件 = '完成') 类型3个数,
(select count(1) from a where 类型4 = '是' and 分级 = t.分级 and 条件 = '完成') 类型4个数,
(select sum(金额) from a where 分级 = t.分级) 金额合计
from a t
group by 分级
select 分级,
Sum(Case 类型1 When '是' then 1 else 0 end) 类型1个数
Sum(Case 类型2 When '是' then 1 else 0 end) 类型2个数
Sum(Case 类型3 When '是' then 1 else 0 end) 类型3个数
Sum(Case 类型4When '是' then 1 else 0 end) 类型4个数
from A
Group by 分级
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-10-15 15:28:32
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([分级] varchar(1),[类型1] varchar(2),[类型2] varchar(2),[类型3] varchar(2),[类型4] varchar(2),[金额] int,[条件] varchar(4))
insert [tb]
select 'A','是','是','否','是',500,'完成' union all
select 'A','是','否','是','是',500,'完成' union all
select 'B','是','是','是','是',100,'完成' union all
select 'B','是','否','是','是',200,'完成'
--------------开始查询--------------------------select
分级,
sum(case [类型1] when '是' then 1 else 0 end) as [类型1],
sum(case [类型2] when '是' then 1 else 0 end) as [类型2],
sum(case [类型3] when '是' then 1 else 0 end) as [类型3],
sum(case [类型4] when '是' then 1 else 0 end) as [类型4],
sum([金额]) as [金额]
from
tb
where
[条件]='完成'
group by
分级
----------------结果----------------------------
/* 分级 类型1 类型2 类型3 类型4 金额
---- ----------- ----------- ----------- ----------- -----------
A 2 1 1 2 1000
B 2 1 2 2 300(2 行受影响)
*/
insert [A]
select 'A','是','是','否','是',500,'完成' union all
select 'A','是','否','是','是',500,'完成' union all
select 'B','是','是','是','是',100,'完成' union all
select 'B','是','否','是','是',200,'完成'select 分级,
(select count(1) from a where 类型1 = '是' and 分级 = t.分级 and 条件 = '完成') 类型1个数,
(select count(1) from a where 类型2 = '是' and 分级 = t.分级 and 条件 = '完成') 类型2个数,
(select count(1) from a where 类型3 = '是' and 分级 = t.分级 and 条件 = '完成') 类型3个数,
(select count(1) from a where 类型4 = '是' and 分级 = t.分级 and 条件 = '完成') 类型4个数,
(select sum(金额) from a where 分级 = t.分级) 金额合计
from a t
group by 分级drop table a/*
分级 类型1个数 类型2个数 类型3个数 类型4个数 金额合计
---- ----------- ----------- ----------- ----------- -----------
A 2 1 1 2 1000
B 2 1 2 2 300(所影响的行数为 2 行)
*/
IF OBJECT_ID('A') IS NOT NULL DROP TABLE A
GO
CREATE TABLE A(分级 varchar(10), 类型1 varchar(10), 类型2 varchar(10), 类型3 varchar(10),类型4 varchar(10),金额 int, 条件 varchar(10))
INSERT INTO A
SELECT
'A', '是' , '是', '否' , '是', 500 ,'完成' UNION ALL select
'A', '是' , '否', '是' , '是', 500 ,'完成' UNION ALL select
'B', '是' , '是', '是' , '是' , 100 ,'完成' UNION ALL select
'B', '是', '否', '是', '是' , 200, '完成'select 分级,
类型1个数=sum(case when 类型1='是' then 1 else 0 end),
类型2个数=sum(case when 类型2='是' then 1 else 0 end),
类型3个数=sum(case when 类型3='是' then 1 else 0 end),
类型4个数=sum(case when 类型4='是' then 1 else 0 end),
金额合计=sum(金额)
from a
where 条件='完成'
group by 分级
分级 类型1个数 类型2个数 类型3个数 类型4个数 金额合计
---------- ----------- ----------- ----------- ----------- -----------
A 2 1 1 2 1000
B 2 1 2 2 300(2 行受影响)