调查结果 中年人(man) 小孩 (boy) 老年人(old) 总计
非常满意 10 50 45 105
满意 45 45 12 102
一般 21 21 21 63
不满意 2 2 3 7
非常不满意 0 0 0 0
总计 78 118 81 287
满意度 12% 2% 32% 43%
不满意度 32% 32% 21% 1%
请教sql高手们,这个结果怎么得到。中年人,小孩,老年人 这些是属于一个字段下的值,总计 是没有字段的。
满意度 是 表中有个字段 PROBLEM_ID (如果值是:1 满意,2:不满意,3 非常不满意,4 一般,5,非常满意) 通过这个字段的值来判断满意的情况, 现在要通过这值,来计算对应的数量跟百分比值。
这里用到了行列转换。 但是不知道怎么实现得到这个结果。望高手们指点,在线等。
------解决方案--------------------
- SQL code
select 调查结果, sum(case when 年龄段 when 'man' then 1 else 0 end) as '中年人', sum(case when 年龄段 when 'boy' then 1 else 0 end) as '小孩', sum(case when 年龄段 when 'old' then 1 else 0 end) as '老年人', count(1) as 总计from tbgroup by 调查结果
------解决方案--------------------
- SQL code
declare @T table([调查结果] varchar(10),[man] int,[boy] int,[old] int)insert @Tselect '非常满意',10,50,45 union allselect '满意',45,45,12 union allselect '一般',21,21,21 union allselect '不满意',2,2,3 union allselect '非常不满意',0,0,0select *,man+boy+old As 总计 from @T union allselect '总计',sum([man]),sum([boy]),sum([old]),sum([man])+sum([boy])+sum([old]) from @T/*调查结果 man boy old 总计---------- ----------- ----------- ----------- -----------非常满意 10 50 45 105满意 45 45 12 102一般 21 21 21 63不满意 2 2 3 7非常不满意 0 0 0 0总计 78 118 81 277*/
------解决方案--------------------
- SQL code
要后面的汇总可以用with with t as (select 调查结果, isnull(sum(case when 年龄类型 ='man' then 1 else 0 end),0) as '中年人', isnull(sum(case when 年龄类型 ='boy' then 1 else 0 end),0) as '小孩', isnull(sum(case when 年龄类型 ='old' then 1 else 0 end),0) as '老年人', count(1) as 总计from tb a right join (select 1 as 调查结果 union all select 2 as 调查结果 union all select 3 as 调查结果 union all select 4 as 调查结果 union all select 5 as 调查结果) b on b.调查结果=a.调查结果group by b.调查结果),t1 as ( select '总计' as 调查结果,sum(中年人) as 中年人,sum(小孩) as 小孩, sum(老年人) as 老年人, sum(中年人)+sum(小孩)+sum(老年人) as 总计 from t),t2 as ( select '满意度' as 调查结果,sum(case when 调查结果!='不满意' and 调查结果!='非常不满意' then 中年人 else 0 end)/sum(中年人) as 中年人,sum(case when 调查结果!='不满意' and 调查结果!='非常不满意' then 孩子 else 0 end)/sum(孩子) as 孩子,sum(case when 调查结果!='不满意' and 调查结果!='非常不满意' then 老年人 else 0 end)/sum(老年人) as 老年人,sum(case when 调查结果!='不满意' and 调查结果!='非常不满意' then 总计 else 0 end)/sum(总计) as 总计 from t ),t3 as ( select '不满意度' as 调查结果,sum(case when 调查结果='不满意' or 调查结果='非常不满意' then 中年人 else 0 end)/sum(中年人) as 中年人,sum(case when 调查结果='不满意' or 调查结果='非常不满意' then 孩子 else 0 end)/sum(孩子) as 孩子,sum(case when 调查结果='不满意' or 调查结果='非常不满意' then 老年人 else 0 end)/sum(老年人) as 老年人,sum(case when 调查结果='不满意' or 调查结果='非常不满意' then 总计 else 0 end)/sum(总计) as 总计 from t )select * from t union allselect * from t1 union allselect * from t2 union allselect * from t3 union all
------解决方案--------------------
- SQL code
create table test([id] int, [AgeLevel] varchar(10),[PROBLEM_ID] int)goinsert testselect 1,'老年人', 1 union allselect 2,'小孩', 2 union allselect 3,'老年人', 3 union allselect 4,'中年人', 4 union allselect 5,'老年人', 5 union allselect 6,'老年人', 1 union allselect 7,'小孩', 2 union allselect 8,'小孩', 3 union allselect 9,'中年人', 4 union allselect 10,'中年人', 5 union allselect 11,'老年人',2select * from testdeclare @sql varchar(max)declare @sql2 varchar(100)declare @sql3 varchar(200)select @sql=isnull(@sql+',','')+' count(case when [AgeLevel]='''+[AgeLevel]+''' then [PROBLEM_ID] end) as '''+[AgeLevel]+'''',@sql2=isnull(@sql2+',','')+'sum(['+[AgeLevel]+']) as '''+[AgeLevel]+'''',@sql3=isnull(@sql3+'+','')+'['+[AgeLevel]+']'from(select distinct [AgeLevel] from test) texec('select *, ([email protected]+') as ''总计'' from ( select (case when [PROBLEM_ID]=1 then ''满意'' when [PROBLEM_ID]=2 then ''不满意'' when [PROBLEM_ID]=3 then ''非常不满意'' when [PROBLEM_ID]=4 then ''一般'' when [PROBLEM_ID]=5 then ''非常满意'' end) as ''调查结果'',[email protected]+' from ( select [PROBLEM_ID],[email protected]+' from test group by [AgeLevel],[PROBLEM_ID] ) t group by [PROBLEM_ID] ) t2')drop table test /*(11 row(s) affected)id AgeLevel PROBLEM_ID----------- ---------- -----------1 老年人 12 小孩 23 老年人 34 中年人 45 老年人 56 老年人 17 小孩 28 小孩 39 中年人 410 中年人 511 老年人 2(11 row(s) affected)调查结果 老年人 小孩 中年人 总计---------- ----------- ----------- ----------- -----------满意 2 0 0 2不满意 1 2 0 3非常不满意 1 1 0 2一般 0 0 2 2非常满意 1 0 1 2(5 row(s) affected)*/