横向的结果好像只能用子查询,纵向的要用union all
横向:
select
(select count(*) from student where age between 10 and 20) as a,
(select count(*) from studen where age between 20 and 30) as b,
.................
纵向:
select '10-20',count(*) from student where age between 10 and 20
union all
select '20-30',count(*) from student where age between 20 and 30
................
1 select a,count(1) from (SELECT (CASE
2 WHEN AGE BETWEEN 10 AND 20 THEN 1
3 WHEN AGE BETWEEN 10 AND 20 THEN 2
4 ELSE 3 END ) AS A
5 FROM student) AS T group by A
没有环境随手写的,你试一试
1楼正解
Select sum(case when age between 10 and 20 then 1 else 0 end) as 学生人数,
sum(case when age between 20 and 30 then 1 else 0 end) as 青年人数,
sum(case when (age >30 and age<10) then 1 else 0 end) as 其他人数
From Student With (nolock)
Where 1=1.....