这个结果很多人想知道吧。
调查结果中年人(man) 小孩 (boy) 老年人(old) 总计
非常满意105045 105
满意4545 12 102
一般2121 21 63
不满意22 3 7
非常不满意000 0
总计78118 81 287
满意度 12% 2% 32% 43%
不满意度32% 32% 21% 1%
请教sql高手们,这个结果怎么得到。中年人,小孩,老年人 这些是属于一个字段下的值,总计 是没有字段的。
满意度 是 表中有个字段 PROBLEM_ID (如果值是:1 满意,2:不满意,3 非常不满意,4 一般,5,非常满意) 通过这个字段的值来判断满意的情况, 现在要通过这值,来计算对应的数量跟百分比值。
这里用到了行列转换。 但是不知道怎么实现得到这个结果。望高手们指点,在线等。
[解决办法]
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 调查结果
[解决办法]
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*/
[解决办法]
要后面的汇总可以用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
[解决办法]
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 *, ('+@sql3+') 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 ''调查结果'','+@sql2+' from ( select [PROBLEM_ID],'+@sql+' 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)*/
[解决办法]
select * from cost---id people problem_id couts1 中年人 1 102 中年人 2 453 中年人 3 214 中年人 4 25 中年人 5 06 小孩 1 507 小孩 2 458 小孩 3 219 小孩 4 210 小孩 5 011 老年人 1 4512 老年人 2 1213 老年人 3 2114 老年人 4 315 老年人 5 0------------下面是代码declare @man int ,@child int ,@old int,@zj int select @man=sum([中年人]), @child=sum([小孩]),@old= sum([老年人]),@zj=sum([总计])from (select sum(case people when '中年人' then couts else 0 end) as '中年人',sum(case people when '小孩' then couts else 0 end)as '小孩',sum(case people when '老年人' then couts else 0 end)as '老年人', sum(couts)'总计'from cost group by problem_id)aselect case problem_id when 1 then '非常满意' when 2 then '满意' when 3 then'一般'when 4 then'不满意'when 5 then '非常不满意'end '调查结果',cast(sum(case people when '中年人' then couts else 0 end)as varchar(20)) as '中年人',cast(sum(case people when '小孩' then couts else 0 end)as varchar(20))as '小孩',cast(sum(case people when '老年人' then couts else 0 end)as varchar(20))as '老年人',cast( sum(couts)as varchar(20))'总计'from cost group by problem_idunion allselect '总计',cast(@man as varchar(20)),cast(@child as varchar(20)),cast(@old as varchar(20)),cast(@zj as varchar(20))union allselect '满意度',cast(cast(100.0*sum([中年人])/@man as decimal(18,2)) as varchar(10))+'%',cast(cast(100.0*sum([小孩])/@child as decimal(18,2))as varchar(10))+'%',cast(cast(100.0*sum([老年人])/@old as decimal(18,2))as varchar(10))+'%',cast(cast(100.0*sum([总计])/@zj as decimal(18,2))as varchar(10))+'%' from (select case problem_id when 1 then '非常满意' when 2 then '满意' when 3 then'一般'when 4 then'不满意'when 5 then '非常不满意'end '调查结果',sum(case people when '中年人' then couts else 0 end) as '中年人',sum(case people when '小孩' then couts else 0 end)as '小孩',sum(case people when '老年人' then couts else 0 end)as '老年人', sum(couts)'总计'from cost group by problem_id)cwhere [调查结果]in('非常满意','满意') union allselect '不满意度',cast(cast(100.0*sum([中年人])/@man as decimal(18,2)) as varchar(10))+'%',cast(cast(100.0*sum([小孩])/@child as decimal(18,2))as varchar(10))+'%',cast(cast(100.0*sum([老年人])/@old as decimal(18,2))as varchar(10))+'%',cast(cast(100.0*sum([总计])/@zj as decimal(18,2))as varchar(10))+'%' from (select case problem_id when 1 then '非常满意' when 2 then '满意' when 3 then'一般'when 4 then'不满意'when 5 then '非常不满意'end '调查结果',sum(case people when '中年人' then couts else 0 end) as '中年人',sum(case people when '小孩' then couts else 0 end)as '小孩',sum(case people when '老年人' then couts else 0 end)as '老年人', sum(couts)'总计'from cost group by problem_id)cwhere [调查结果]in('非常不满意','不满意') ----结果:调查结果 中年人 小孩 老人 总计非常满意 10 50 45 105满意 45 45 12 102一般 21 21 21 63不满意 2 2 3 7非常不满意 0 0 0 0总计 78 118 81 277满意度 70.51% 80.51% 70.37% 74.73%不满意度 2.56% 1.69% 3.70% 2.53%