group by 统计时,有些值要求合并在一起统计,
比如
group by ageage字段出现的值为{10,20,30,40,50,60,70}
但要求10,20,30放在一起统计(该组取名为“年轻”),
40,50放在一起统计(该组取名为“中年”),
其余取名为“老年”,如何实现?
解决方案 »
- 求一个存储过程,急
- 关于多个表实现左链接的问题,比较困惑,请高人指点
- 数据库无法按文件-文件组备份
- SQL备份出错,搞了两个星期了还没搞好,请指点!!!
- 将sql拆分好的字符串,如何和插入到同一行内?
- 我刚刚学数据库,我想用WINDOWS SDK(不用MFC)做界面,用SQL做数据库部分,怎么办??
- SQL SERVER中查询当前网络里SQL SERVER服务的存储过程是什么?
- 急急急,急呀,找不到SQL SERVER 2000的安装密码,帮帮我吧!!!!!
- 对于用vfp6.0编译的exe文件,怎样才能不被人反编译出来?
- 请教各位,关于数据库备份的问题
- 谁有sql6.5 的 Rldblib.lib
- sql 2005 Development版能在Server版操作系统上安装吗
DECLARE @t TABLE(ID int PRIMARY KEY,col decimal(10,2))
INSERT @t SELECT 1 ,26.21
UNION ALL SELECT 2 ,88.19
UNION ALL SELECT 3 , 4.21
UNION ALL SELECT 4 ,76.58
UNION ALL SELECT 5 ,58.06
UNION ALL SELECT 6 ,53.01
UNION ALL SELECT 7 ,18.55
UNION ALL SELECT 8 ,84.90
UNION ALL SELECT 9 ,95.60--统计
SELECT a.Description,
Record_count=COUNT(b.ID),
[Percent]=CASE
WHEN Counts=0 THEN '0.00%'
ELSE CAST(CAST(
COUNT(b.ID)*100./c.Counts
as decimal(10,2)) as varchar)+'%'
END
FROM(
SELECT sid=1,a=NULL,b=30 ,Description='<30' UNION ALL
SELECT sid=2,a=30 ,b=60 ,Description='>=30 and <60' UNION ALL
SELECT sid=3,a=60 ,b=75 ,Description='>=60 and <75' UNION ALL
SELECT sid=4,a=75 ,b=95 ,Description='>=75 and <95' UNION ALL
SELECT sid=5,a=95 ,b=NULL,Description='>=95'
)a LEFT JOIN @t b
ON (b.col<a.b OR a.b IS NULL)
AND(b.col>=a.a OR a.a IS NULL)
CROSS JOIN(
SELECT COUNTS=COUNT(*) FROM @t
)c
GROUP BY a.Description,a.sid,c.COUNTS
ORDER BY a.sid
/*--结果:
Description Record_count Percent
------------------- ------------------ ----------------------
<30 3 33.33%
>=30 and <60 2 22.22%
>=60 and <75 0 0.00%
>=75 and <95 3 33.33%
>=95 1 11.11%
--*/
WHEN age>=40 AND age<70 THEN '中年'
ELSE age>=70 THEN '老年'
END,
COUNT(*) AS 人数
FROM tb_name
GROUP BY
CASE WHEN age>=10 AND age<40 THEN '年龄'
WHEN age>=40 AND age<70 THEN '中年'
ELSE age>=70 THEN '老年'
END
select age as 年轻 from sttable where age in(10,20,30)
WHEN age>=40 AND age<70 THEN '中年'
ELSE '老年'
END,
COUNT(*) AS 人数
FROM @t
GROUP BY
CASE WHEN age>=10 AND age<40 THEN '年龄'
WHEN age>=40 AND age<70 THEN '中年'
ELSE '老年'
END
SELECT CASE WHEN age in (10,20,30) THEN '年轻'
WHEN age in (40,50) THEN '中年'
ELSE '老年'
END,
COUNT(*) AS 人数
FROM tb_name
GROUP BY
CASE WHEN age in (10,20,30) THEN '年轻'
WHEN age in (40,50) THEN '中年'
ELSE '老年'
END
alert table add 年龄组 varchar(20)
update table set 年龄组='青年' where age<=30
update table set 年龄组='老年' where age >30
select 年龄组,sum(age) from table where 年龄组='青年' group by 年龄组 union select 年龄组,sum(age) from table where 年龄组='老年' group by 年龄组
先在表table中加一列名为'年龄组',然后统计.
alert table add 年龄组 varchar(20)
update table set 年龄组='青年' where age <=30
update table set 年龄组='青年' where age>30 and age <=50
update table set 年龄组='老年' where age >50
select 年龄组,sum(age) from table where 年龄组='少年' group by 年龄组 union select 年龄组,sum(age) from table where 年龄组='青年' group by 年龄组 union select 年龄组,sum(age) from table where 年龄组='老年' group by 年龄组
when age between 40 and 50 then 'bb'
else 'cc' end),count(*),count(1)/(select cast ( count(*) as float )from a )
from a group by (case when age between 10 and 30 then 'aa'
when age between 40 and 50 then 'bb'
else 'cc' end)
union
select count(*) as 中年人数 from tableName where age >=40 and age <=50
union
select count(*) as 老年人数 from tableName where age not age >=60
union
select count(*) as 中年人数 from tableName where age >=40 and age <=50
union
select count(*) as 老年人数 from tableName where age >=60