实现后就是这样的
说一下我的原表:name(车名),tonage(载客量)
然后就是把载客量放到对应的区间上
SELECT
name,
CASE tonage
WHEN tonage < 10 THEN tonage
ELSE NULL
END tonage_10,
CASE tonage
WHEN tonage >= 10 AND tonage< 30 THEN tonage
ELSE NULL
END tonage_10_30,
CASE tonage
WHEN tonage >= 30 AND tonage< 90 THEN tonage
ELSE NULL
END tonage_30_90,
CASE tonage
WHEN tonage >= 90 AND tonage< 100 THEN tonage
ELSE NULL
END tonage_90_100,
CASE tonage
WHEN tonage >= 100 THEN tonage
ELSE NULL
END tonage_100
FROM TABLE_NAME
这个不应该用sql来实现吧 随便用一种后台语言都可以完美搞定
if else 或者 switch 都行
判断在哪个区间 然后insert
用case when语法
既然是做报表也就是相当于导出excel,你可以后台直接取数据,判断下>10 在excel的C4写入数据 10到30之间 在C5写数据。C4.C5我只是举例子,具体自己对照。
SELECT name,
count(case when tonage=10 then 1 else null end) "10",
count(case when tonage>10 and tongage count(case when tonage>=30 and tongage count(case when tonage=100 then 1 else null end) "100",
count(case when tonage>100 and tongage<150 then 1 else null end) "100~150",
from 替换为实际表名 GROUP BY name;
-- 格式化一下
SELECT name,
count(case when tonage=10 then 1 else null end) "10",
count(case when tonage>10 and tongage count(case when tonage>=30 and tongage count(case when tonage=100 then 1 else null end) "100",
count(case when tonage>100 and tongage<150 then 1 else null end) "100~150",
from 替换为实际表名 group by name;
你这本身很简单的事情非要复杂化
全查出来以后if判断set到一个对象,最后返回这个对象的集合
创建表,例如,create table Person
(
id int,
name nvarchar(5),
sex char(2),
age int,
job nvarchar(20)
);
insert into Person (id,name,sex,age,job)
values
(1,'李小明','男',25,'C#程序员'),
(2,'王小红','女',19,'学生'),
(3,'李小明','男',22,'学生');