查询统计

来源:百度知道 编辑:UC知道 时间:2024/06/24 11:39:37
这个sql语句应该怎么写
姓名 缴费金额 学员来源
a 3000 dd
b 3050 cc
c 4025 bb
d 6000 dd
e 2000 aa

统计结果

学员来源 人数 3000以下 3000-4000 4000-5000 5000以上
dd 2 0 1 0 1

bb 1 0 0 1 0
cc 1 0 0 1 0
aa 1 1 0 0 0

不知道你是什么数据库~
现提供oracle 和 infromix 版的SQL , sql server 应该也行吧~

select 学员来源 ,
count(*) p,
sum(case when 缴费金额 < 3000 then 1 else 0 end) d3,
sum(case when 缴费金额 >= 3000 and 缴费金额 <= 4000 then 1 else 0 end) d3_4,
sum(case when 缴费金额 >= 4000 and 缴费金额 <= 5000 then 1 else 0 end) d4_5,
sum(case when 缴费金额 > 5000 then 1 else 0 end) d5
from 表
group by 学员来源;

OK

select 学员来源,count(stu_id),sum(case when money<3000 then 1 else 0 end) 3000以下,sum(case when money between 3000 and 4000 then 1 else 0 end) 3000-4000,sum(case when money between 4000 and 5000 then 1 else 0 end) 4000-5000,sum(case when money>5000 then 1 else 0 end) 5000以上 from 表名 group by 学员来源;