学生表(年级,班别,姓名)
我想得到这样的视图(一班数,二班数,三班数,四班数,五班数,六班数)
一班数为:一年级的班数。
我用count(distinct 班别),然后不知道怎么加年级的条件限制!
我想得到这样的视图(一班数,二班数,三班数,四班数,五班数,六班数)
一班数为:一年级的班数。
我用count(distinct 班别),然后不知道怎么加年级的条件限制!
解决方案 »
- 存储过程删除关联表 。。。。急 在线等!
- sql server 表分区的问题(水平分区)
- 如何取得学历管理表中的最高学历
- 自定义函数里面无法进行插入操作?
- 帮忙解决错误 '80004005',Optional feature not implemented
- rand()为什么在函数中用不了,急用!
- SQL setup以停止服务
- IIS7应用程序池自动停止
- 为什么OLTP索引有碎片有利, olap不利? 那还用对OLTP 整理碎片吗?
- 请问谁能配合企业管理器作一个图文并茂的关于WORD文件或者是TEXT文本文件导入到数据库教程啊
- 关于创建临时表同时使用后会删除掉
- SQL Server2005 系统表的问题
insert into tb
select '1','1','zhang'
union all
select '1','2','wang'
union all
select '1','3','li'
union all
select '1','3','li'
union all
select '2','1','liang'
union all
select '2','2','he'
union all
select '2','3','chen'
静态
select a.年级,
[一班数]=sum(case [班别] when '1' then 1 else 0 end),
[二班数]=sum(case [班别] when '2' then 1 else 0 end),
[三班数]=sum(case [班别] when '3' then 1 else 0 end),
[四班数]=sum(case [班别] when '4' then 1 else 0 end),
[五班数]=sum(case [班别] when '5' then 1 else 0 end),
[六班数]=sum(case [班别] when '6' then 1 else 0 end)
from tb a
group by 年级
动态
declare @sql varchar(8000)
set @sql='select 年级'
select @sql=@sql+',['+班别+']=sum(case 班别 when '''+班别+''' then 1 else 0 end)' from tb group by 班别
exec(@sql+' from tb group by 年级')
create view t_v
as
select 年级 , count(distinct 班别)
from 学生表
group by 年级
我不小心多插入了一条重复的记录,即select '1','3
insert into tb
select '1','1','zhang'
union all
select '1','2','wang'
union all
select '1','3','li'
union all
select '1','3','li'
union all
select '2','1','liang'
union all
select '2','2','he'
union all
select '2','3','chen'
CREATE FUNCTION p_getClass(@g int)
RETURNS INT
AS
BEGIN
DECLARE @C INT
SELECT @C=COUNT(DISTINCT 班别 ) FROM TB WHERE 年级=@g
RETURN @C
ENDSELECT dbo.p_getClass(1) AS 一班数 , dbo.p_getClass(2) AS 二班数 , dbo.p_getClass(3) AS 三班数 , dbo.p_getClass(4) AS 四班数 , dbo.p_getClass(5) AS 五班数 , dbo.p_getClass(6) AS 六班数
[一班数]=sum(case [年级] when 1 then 1 else 0 end),
[二班数]=sum(case [年级] when 2 then 1 else 0 end),
[三班数]=sum(case [年级] when 3 then 1 else 0 end),
[四班数]=sum(case [年级] when 4 then 1 else 0 end),
[五班数]=sum(case [年级] when 5 then 1 else 0 end),
[六班数]=sum(case [年级] when 6 then 1 else 0 end)
from 学生表
group by 年级
[一班数]=sum(distinct(case [班别] when '1' then 1 else 0 end)) ,
[二班数]=sum(distinct(case [班别] when '2' then 1 else 0 end)),
[三班数]=sum(distinct(case [班别] when '3' then 1 else 0 end)),
[四班数]=sum(distinct(case [班别] when '4' then 1 else 0 end)),
[五班数]=sum(distinct(case [班别] when '5' then 1 else 0 end)),
[六班数]=sum((case [班别] when '6' then 1 else 0 end))
from tb a
group by 年级
as
select 一班数 = sum(case 年级 when 2 then 班数 else 0 end),
二班数 = sum(case 年级 when 2 then 班数 else 0 end),
三班数 = sum(case 年级 when 3 then 班数 else 0 end),
四班数 = sum(case 年级 when 4 then 班数 else 0 end),
五班数 = sum(case 年级 when 5 then 班数 else 0 end),
六班数 = sum(case 年级 when 6 then 班数 else 0 end)
from
(
select 年级 , count(distinct 班别) 班数
from 学生表
group by 年级
) a
select a.年级,
[一班数]=sum(distinct(case [班别] when '1' then 1 else 0 end)) ,
[二班数]=sum(distinct(case [班别] when '2' then 1 else 0 end)),
[三班数]=sum(distinct(case [班别] when '3' then 1 else 0 end)),
[四班数]=sum(distinct(case [班别] when '4' then 1 else 0 end)),
[五班数]=sum(distinct(case [班别] when '5' then 1 else 0 end)),
[六班数]=sum(distinct(case [班别] when '6' then 1 else 0 end))
from tb a
group by 年级
count(distinct 班别) [when 年级=1]
count(distinct 班别) [when 年级=2]
count(distinct 班别) [when 年级=3]
count(distinct 班别) [when 年级=4]
count(distinct 班别) [when 年级=5]
count(distinct 班别) [when 年级=6]但不知道要怎么样实现
不知道能不能把 when 年级=1 这个条件加到 count语句中
select [一班数]=(select count(*)
from a
where 年级='1'
group by 年级,班别)
……
上面只求了一年级的班级数,不知道行不行,楼主自己试试吧
select [一班数]=(select count(*)
from a
where 年级='1'
group by 年级,班别)
[二班数]=(select count(*)
from a
where 年级='2'
group by 年级,班别)
……from ……
as
select 年级,count(distinct 班别) from 学生表 group by 年级
select 年级,
[一班数]=sum(case [班别] when '1' then 1 else 0 end) ,
[二班数]=sum(case [班别] when '2' then 1 else 0 end),
[三班数]=sum(case [班别] when '3' then 1 else 0 end),
[四班数]=sum(case [班别] when '4' then 1 else 0 end),
[五班数]=sum(case [班别] when '5' then 1 else 0 end),
[六班数]=sum(case [班别] when '6' then 1 else 0 end)
from class
group by 年级结果:
一班
1 3 3 0 0 0 0
2 2 1 1 0 0 0
6 1 0 0 0 0 0