如有
tab
ID Name Price
1 A 5
2 B 8
3 C 12
4 D 22
5 E 25
6 F 7
7 G 20--------------------------
结果为
Price count
0-10 3
11-20 2
21-30 3
谢谢
tab
ID Name Price
1 A 5
2 B 8
3 C 12
4 D 22
5 E 25
6 F 7
7 G 20--------------------------
结果为
Price count
0-10 3
11-20 2
21-30 3
谢谢
CASE WHEN Price BETWEEN 1 AND 10 THEN '1~10'
WHEN Price BETWEEN 11 AND 20 THEN '11~20'
WHEN Price BETWEEN 21 AND 30 THEN '21~30'
ELSE '>30'
END AS Price,
COUNT(*) AS [Count]
FROM tb
GROUP BY
CASE WHEN Price BETWEEN 1 AND 10 THEN '1~10'
WHEN Price BETWEEN 11 AND 20 THEN '11~20'
WHEN Price BETWEEN 21 AND 30 THEN '21~30'
ELSE '>30'
END
insert into tb select 1,'A',5
insert into tb select 2,'B',8
insert into tb select 3,'C',12
insert into tb select 4,'D',22
insert into tb select 5,'E',25
insert into tb select 6,'F',7
insert into tb select 7,'G',20
go
select ltrim(floor((price-1)/10))+'1'+'~'+ltrim(floor((price-1)/10)+1)+'0' as p,count(1) as c from tb group by ltrim(floor((price-1)/10))+'1'+'~'+ltrim(floor((price-1)/10)+1)+'0'
go
drop table tb
/*
p c
--------------------------- -----------
01~10 3
11~20 2
21~30 2
*/