testId ageRange
4987 10年
3730 11年
3803 12年
3580 13年
3027 14年
3487 15年
2587 16年
2356 17年
2226 18年
2082 19年
2936 1年
2633 20年
1449 21年
1329 22年
1305 23年
913 24年
997 25年
816 26年
832 27年
740 28年
671 29年
3119 2年
595 30年
278 31年
228 32年
157 33年
89 34年
62 35年
35 36年
10 37年
7 38年
8 39年
2712 3年
5 40年
68 40年以上
1825 4年
2365 5年
2235 6年
2464 7年
2871 8年
3665 9年
8472 其他需求:按ageRange 以'5年','5年以上至10年','10年以上至15年','15年以上至20年','20年以上' 来统计
结果如下
ageRange newcount
5年 6
5年以上至10年 5
10年以上至15年 5
15年以上至20年 5
20年以上 21
求统计sql语句,谢谢!
if object_id('[tb]') is not null drop table [tb]
create table [tb]([testId] int,[ageRange] varchar(8))
go
insert [tb]
select 4987,'10年' union all
select 3730,'11年' union all
select 3803,'12年' union all
select 3580,'13年' union all
select 3027,'14年' union all
select 3487,'15年' union all
select 2587,'16年' union all
select 2356,'17年' union all
select 2226,'18年' union all
select 2082,'19年' union all
select 2936,'1年' union all
select 2633,'20年' union all
select 1449,'21年' union all
select 1329,'22年' union all
select 1305,'23年' union all
select 913,'24年' union all
select 997,'25年' union all
select 816,'26年' union all
select 832,'27年' union all
select 740,'28年' union all
select 671,'29年' union all
select 3119,'2年' union all
select 595,'30年' union all
select 278,'31年' union all
select 228,'32年' union all
select 157,'33年' union all
select 89,'34年' union all
select 62,'35年' union all
select 35,'36年' union all
select 10,'37年' union all
select 7,'38年' union all
select 8,'39年' union all
select 2712,'3年' union all
select 5,'40年' union all
select 68,'40年以上' union all
select 1825,'4年' union all
select 2365,'5年' union all
select 2235,'6年' union all
select 2464,'7年' union all
select 2871,'8年' union all
select 3665,'9年' union all
select 8472,'其他'select case when ageRange <= 5 then '5年' when ageRange > 5 and ageRange <= 10 then '5年以上至10年'
when ageRange >= 10 and ageRange <=15 then '10年以上至15年' when ageRange > 15 and ageRange <= 20 then
'15年以上至20年' when ageRange >= 20 then '20年以上' end as ageRange,
count(1) as newcount
from
(
select
(case ageRange when '其他' then 0 else cast(substring(ageRange,1,patindex('%[吖-唑]%',ageRange) - 1) as int) end) as ageRange
from [tb]
) t
group by
case when ageRange <= 5 then '5年' when ageRange > 5 and ageRange <= 10 then '5年以上至10年'
when ageRange >= 10 and ageRange <=15 then '10年以上至15年' when ageRange > 15 and ageRange <= 20 then
'15年以上至20年' when ageRange >= 20 then '20年以上' end
ageRange newcount
-------------- -----------
10年以上至15年 5
15年以上至20年 5
20年以上 21
5年 6
5年以上至10年 5(所影响的行数为 5 行)
-- Author :SQL77(只为思齐老)
-- Date :2010-03-10 11:17:23
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([testId] int,[ageRange] varchar(8))
insert #TB
select 4987,'10年' union all
select 3730,'11年' union all
select 3803,'12年' union all
select 3580,'13年' union all
select 3027,'14年' union all
select 3487,'15年' union all
select 2587,'16年' union all
select 2356,'17年' union all
select 2226,'18年' union all
select 2082,'19年' union all
select 2936,'1年' union all
select 2633,'20年' union all
select 1449,'21年' union all
select 1329,'22年' union all
select 1305,'23年' union all
select 913,'24年' union all
select 997,'25年' union all
select 816,'26年' union all
select 832,'27年' union all
select 740,'28年' union all
select 671,'29年' union all
select 3119,'2年' union all
select 595,'30年' union all
select 278,'31年' union all
select 228,'32年' union all
select 157,'33年' union all
select 89,'34年' union all
select 62,'35年' union all
select 35,'36年' union all
select 10,'37年' union all
select 7,'38年' union all
select 8,'39年' union all
select 2712,'3年' union all
select 5,'40年' union all
select 68,'40年以上' union all
select 1825,'4年' union all
select 2365,'5年' union all
select 2235,'6年' union all
select 2464,'7年' union all
select 2871,'8年' union all
select 3665,'9年' union all
select 8472,'其他'
--------------开始查询--------------------------
SELECT
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange) BETWEEN 0 AND 5 THEN 1 ELSE 0 END)AS '0-5',
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange) BETWEEN 6 AND 10 THEN 1 ELSE 0 END)AS '6-10',
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange) BETWEEN 11 AND 15 THEN 1 ELSE 0 END)AS '0-5',
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange) BETWEEN 16 AND 25 THEN 1 ELSE 0 END)AS '0-5',
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange) BETWEEN 26 AND 35 THEN 1 ELSE 0 END)AS '0-5',
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange) BETWEEN 36 AND 45 THEN 1 ELSE 0 END)AS '0-5'
FROM
(
select testId,
REPLACE(REPLACE(REPLACE(ageRange,'年','.0'),'以上','1'),'其他','0') AS ageRange
from #TB
)AS T----------------结果----------------------------
/* (所影响的行数为 42 行)0-5 6-10 0-5 0-5 0-5 0-5
----------- ----------- ----------- ----------- ----------- -----------
6 5 5 10 10 6(所影响的行数为 1 行)
*/
-- Author :SQL77(只为思齐老)
-- Date :2010-03-10 11:17:23
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([testId] int,[ageRange] varchar(8))
insert #TB
select 4987,'10年' union all
select 3730,'11年' union all
select 3803,'12年' union all
select 3580,'13年' union all
select 3027,'14年' union all
select 3487,'15年' union all
select 2587,'16年' union all
select 2356,'17年' union all
select 2226,'18年' union all
select 2082,'19年' union all
select 2936,'1年' union all
select 2633,'20年' union all
select 1449,'21年' union all
select 1329,'22年' union all
select 1305,'23年' union all
select 913,'24年' union all
select 997,'25年' union all
select 816,'26年' union all
select 832,'27年' union all
select 740,'28年' union all
select 671,'29年' union all
select 3119,'2年' union all
select 595,'30年' union all
select 278,'31年' union all
select 228,'32年' union all
select 157,'33年' union all
select 89,'34年' union all
select 62,'35年' union all
select 35,'36年' union all
select 10,'37年' union all
select 7,'38年' union all
select 8,'39年' union all
select 2712,'3年' union all
select 5,'40年' union all
select 68,'40年以上' union all
select 1825,'4年' union all
select 2365,'5年' union all
select 2235,'6年' union all
select 2464,'7年' union all
select 2871,'8年' union all
select 3665,'9年' union all
select 8472,'其他'
--------------开始查询--------------------------
SELECT
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange) BETWEEN 0 AND 5 THEN 1 ELSE 0 END)AS '5年',
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange) BETWEEN 6 AND 10 THEN 1 ELSE 0 END)AS '5以上到10',
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange) BETWEEN 11 AND 15 THEN 1 ELSE 0 END)AS '10以上到15',
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange) BETWEEN 16 AND 20 THEN 1 ELSE 0 END)AS '15以上到20',
SUM(CASE WHEN CONVERT(DEC(18,2),ageRange)>=21 THEN 1 ELSE 0 END)AS '20年以上'FROM
(
select testId,
REPLACE(REPLACE(REPLACE(ageRange,'年','.0'),'以上','1'),'其他','1') AS ageRange
from #TB
)AS T
(所影响的行数为 42 行)5年 5以上到10 10以上到15 15以上到20 20年以上
----------- ----------- ----------- ----------- -----------
6 5 5 5 21(所影响的行数为 1 行)
-- Author :SQL77(只为思齐老)
-- Date :2010-03-10 11:17:23
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([testId] int,[ageRange] varchar(8))
insert #TB
select 4987,'10年' union all
select 3730,'11年' union all
select 3803,'12年' union all
select 3580,'13年' union all
select 3027,'14年' union all
select 3487,'15年' union all
select 2587,'16年' union all
select 2356,'17年' union all
select 2226,'18年' union all
select 2082,'19年' union all
select 2936,'1年' union all
select 2633,'20年' union all
select 1449,'21年' union all
select 1329,'22年' union all
select 1305,'23年' union all
select 913,'24年' union all
select 997,'25年' union all
select 816,'26年' union all
select 832,'27年' union all
select 740,'28年' union all
select 671,'29年' union all
select 3119,'2年' union all
select 595,'30年' union all
select 278,'31年' union all
select 228,'32年' union all
select 157,'33年' union all
select 89,'34年' union all
select 62,'35年' union all
select 35,'36年' union all
select 10,'37年' union all
select 7,'38年' union all
select 8,'39年' union all
select 2712,'3年' union all
select 5,'40年' union all
select 68,'40年以上' union all
select 1825,'4年' union all
select 2365,'5年' union all
select 2235,'6年' union all
select 2464,'7年' union all
select 2871,'8年' union all
select 3665,'9年' union all
select 8472,'其他'
--------------开始查询--------------------------
SELECT
CASE
WHEN FLOOR((ageRange-1)/5)=0 THEN '0-5'
WHEN FLOOR((ageRange-1)/5)=1 THEN '6-10'
WHEN FLOOR((ageRange-1)/5)=2 THEN '11-15'
WHEN FLOOR((ageRange-1)/5)=3 THEN '16-2020以上'
WHEN FLOOR((ageRange-1)/5)>=4 THEN '20以上' END AS A,COUNT(1)
FROM
(
select
CONVERT(DEC(18,2),
REPLACE(REPLACE(REPLACE(ageRange,'年','.0'),'以上','1'),'其他','1')) AS ageRange
from #TB
) T
GROUP BY
CASE
WHEN FLOOR((ageRange-1)/5)=0 THEN '0-5'
WHEN FLOOR((ageRange-1)/5)=1 THEN '6-10'
WHEN FLOOR((ageRange-1)/5)=2 THEN '11-15'
WHEN FLOOR((ageRange-1)/5)=3 THEN '16-2020以上'
WHEN FLOOR((ageRange-1)/5)>=4 THEN '20以上' END
----------------结果----------------------------
/* (所影响的行数为 42 行)A
----------- -----------
0-5 6
11-15 5
16-2020以上 5
20以上 21
6-10 5(所影响的行数为 5 行)
*/竖着的
楼主的这个问题我也遇到过,一样的情况
select 4987,'10年' union all
select 3730,'11年' union all
select 3803,'12年' union all
select 3580,'13年' union all
select 3027,'14年' union all
select 3487,'15年' union all
select 2587,'16年' union all
select 2356,'17年' union all
select 2226,'18年' union all
select 2082,'19年' union all
select 2936,'1年' union all
select 2633,'20年' union all
select 1449,'21年' union all
select 1329,'22年' union all
select 1305,'23年' union all
select 913,'24年' union all
select 997,'25年' union all
select 816,'26年' union all
select 832,'27年' union all
select 740,'28年' union all
select 671,'29年' union all
select 3119,'2年' union all
select 595,'30年' union all
select 278,'31年' union all
select 228,'32年' union all
select 157,'33年' union all
select 89,'34年' union all
select 62,'35年' union all
select 35,'36年' union all
select 10,'37年' union all
select 7,'38年' union all
select 8,'39年' union all
select 2712,'3年' union all
select 5,'40年' union all
select 68,'40年以上' union all
select 1825,'4年' union all
select 2365,'5年' union all
select 2235,'6年' union all
select 2464,'7年' union all
select 2871,'8年' union all
select 3665,'9年' union all
select 8472,'其他'
SELECT * FROM
(
SELECT
case when abs(x)<=5 then '5年'
when abs(x)>5 and abs(x)<=10 then '5年以上至10年'
when abs(x)>10 and abs(x)<=15 then '10年以上至15年'
when abs(x)>15 and abs(x)<=20 then '15年以上至20年'
else '20年以上' END a,
count(1) b
FROM
(
SELECT CASE WHEN charindex('年以上',ageRange)>0 THEN '-'+replace(ageRange,'年以上','') else replace(replace(ageRange,'年',''),'其他','0') end x FROM @a
)aa GROUP BY case when abs(x)<=5 then '5年'
when abs(x)>5 and abs(x)<=10 then '5年以上至10年'
when abs(x)>10 and abs(x)<=15 then '10年以上至15年'
when abs(x)>15 and abs(x)<=20 then '15年以上至20年'
else '20年以上' END
)bb
ORDER BY CASE WHEN charindex('年',a)=2 THEN '0'+a else a end
--result
/*
a b
-------------- -----------
5年 6
5年以上至10年 5
10年以上至15年 5
15年以上至20年 5
20年以上 21(所影响的行数为 5 行)
*/
-- Author :SQL77(只为思齐老)
-- Date :2010-03-10 11:17:23
-- Version:
-- Microsoft SQL Server 2000 - 8.00.194 (Intel X86)
-- Aug 6 2000 00:57:48
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go
create table #TB([testId] int,[ageRange] varchar(8))
insert #TB
select 4987,'10年' union all
select 3730,'11年' union all
select 3803,'12年' union all
select 3580,'13年' union all
select 3027,'14年' union all
select 3487,'15年' union all
select 2587,'16年' union all
select 2356,'17年' union all
select 2226,'18年' union all
select 2082,'19年' union all
select 2936,'1年' union all
select 2633,'20年' union all
select 1449,'21年' union all
select 1329,'22年' union all
select 1305,'23年' union all
select 913,'24年' union all
select 997,'25年' union all
select 816,'26年' union all
select 832,'27年' union all
select 740,'28年' union all
select 671,'29年' union all
select 3119,'2年' union all
select 595,'30年' union all
select 278,'31年' union all
select 228,'32年' union all
select 157,'33年' union all
select 89,'34年' union all
select 62,'35年' union all
select 35,'36年' union all
select 10,'37年' union all
select 7,'38年' union all
select 8,'39年' union all
select 2712,'3年' union all
select 5,'40年' union all
select 68,'40年以上' union all
select 1825,'4年' union all
select 2365,'5年' union all
select 2235,'6年' union all
select 2464,'7年' union all
select 2871,'8年' union all
select 3665,'9年' union all
select 8472,'其他'
--------------开始查询--------------------------
SELECT
CASE
WHEN FLOOR((ageRange-1)/5)=0 THEN '0-5'
WHEN FLOOR((ageRange-1)/5)=1 THEN '6-10'
WHEN FLOOR((ageRange-1)/5)=2 THEN '11-15'
WHEN FLOOR((ageRange-1)/5)=3 THEN '16-20以上'
WHEN FLOOR((ageRange-1)/5)>=4 THEN '20以上' END AS A,COUNT(1)
FROM
(
select
CONVERT(DEC(18,2),
REPLACE(REPLACE(REPLACE(ageRange,'年','.0'),'以上','1'),'其他','1')) AS ageRange
from #TB
) T
GROUP BY
CASE
WHEN FLOOR((ageRange-1)/5)=0 THEN '0-5'
WHEN FLOOR((ageRange-1)/5)=1 THEN '6-10'
WHEN FLOOR((ageRange-1)/5)=2 THEN '11-15'
WHEN FLOOR((ageRange-1)/5)=3 THEN '16-20以上'
WHEN FLOOR((ageRange-1)/5)>=4 THEN '20以上' END ORDER BY MIN(FLOOR((ageRange-1)/5))----------------结果----------------------------
/* (所影响的行数为 42 行)A
--------- -----------
0-5 6
6-10 5
11-15 5
16-20以上 5
20以上 21(所影响的行数为 5 行)
*/加个排序
--表结构及记录
if object_id('[tb]') is not null drop table [tb]
create table tb(testId int,ageRange varchar(8))
go
insert tb
select 4987,'10年' union all
select 3730,'11年' union all
select 3803,'12年' union all
select 3580,'13年' union all
select 3027,'14年' union all
select 3487,'15年' union all
select 2587,'16年' union all
select 2356,'17年' union all
select 2226,'18年' union all
select 2082,'19年' union all
select 2936,'1年' union all
select 2633,'20年' union all
select 1449,'21年' union all
select 1329,'22年' union all
select 1305,'23年' union all
select 913,'24年' union all
select 997,'25年' union all
select 816,'26年' union all
select 832,'27年' union all
select 740,'28年' union all
select 671,'29年' union all
select 3119,'2年' union all
select 595,'30年' union all
select 278,'31年' union all
select 228,'32年' union all
select 157,'33年' union all
select 89,'34年' union all
select 62,'35年' union all
select 35,'36年' union all
select 10,'37年' union all
select 7,'38年' union all
select 8,'39年' union all
select 2712,'3年' union all
select 5,'40年' union all
select 68,'40年以上' union all
select 1825,'4年' union all
select 2365,'5年' union all
select 2235,'6年' union all
select 2464,'7年' union all
select 2871,'8年' union all
select 3665,'9年' union all
select 8472,'其他'--语句
select (case when cast(substring(ageRange,1,charindex('年',ageRange)-1) as int)<=5 then '5年以上'
when cast(substring(ageRange,1,charindex('年',ageRange)-1) as int) between 6 and 10 then '5年以上至10年'
when cast(substring(ageRange,1,charindex('年',ageRange)-1) as int) between 11 and 15 then '10年以上至15年'
when cast(substring(ageRange,1,charindex('年',ageRange)-1) as int) between 16 and 20 then '15年以上至20年'
when cast(substring(ageRange,1,charindex('年',ageRange)-1) as int)>20 then '20年以上'
end) as ageRange,
count(*) as newcount
from tb
where ageRange<>'其他'
group by (case when cast(substring(ageRange,1,charindex('年',ageRange)-1) as int)<=5 then '5年以上'
when cast(substring(ageRange,1,charindex('年',ageRange)-1) as int) between 6 and 10 then '5年以上至10年'
when cast(substring(ageRange,1,charindex('年',ageRange)-1) as int) between 11 and 15 then '10年以上至15年'
when cast(substring(ageRange,1,charindex('年',ageRange)-1) as int) between 16 and 20 then '15年以上至20年'
when cast(substring(ageRange,1,charindex('年',ageRange)-1) as int)>20 then '20年以上' end)
union all
select ageRange,count(*) newcount
from tb
where ageRange='其他'
group by ageRange--结果
ageRange newcount
10年以上至15年 5
15年以上至20年 5
20年以上 21
5年以上 5
5年以上至10年 5
其他 1
(case ageRange when '其他' then 0 else cast(substring(ageRange,1,patindex('%[吖-唑]%',ageRange) - 1) as int) end) as ageRange
from [tb]
)
楼主的这个问题我也遇到过,一样的情况