一.我们经常要做数据统计的工作,如下示例统计各部门每月的工作业绩
table1(月份, 部门, 业绩)
mon dep yj
------------------------------------------------------------
1 1 10
1 2 10
1 3 5
2 2 8
2 4 9
3 3 8
------------------------------------------------------------
table2(部门, 部门名称)
dep dname
------------------------------------------------------------
1 A 业务部
2 B业务部
3 C 业务部
4 D业务部
------------------------------------------------------------
table3 (结果)
部门 一月份 二月份 三月份
------------------------------------------------------------
1 10 0 0
2 10 8 0
3 0 5 8
4 0 0 9
------------------------------------------------------------
select a.dep,
sum(case when b.mon=1 then b.yj else 0 end) as '一月份',
sum(case when b.mon=2 then b.yj else 0 end) as '二月份',
sum(case when b.mon=3 then b.yj else 0 end) as '三月份',
sum(case when b.mon=4 then b.yj else 0 end) as '四月份',
sum(case when b.mon=5 then b.yj else 0 end) as '五月份',
sum(case when b.mon=6 then b.yj else 0 end) as '六月份',
sum(case when b.mon=7 then b.yj else 0 end) as '七月份',
sum(case when b.mon=8 then b.yj else 0 end) as '八月份',
sum(case when b.mon=9 then b.yj else 0 end) as '九月份',
sum(case when b.mon=10 then b.yj else 0 end) as '十月份',
sum(case when b.mon=11 then b.yj else 0 end) as '十一月份',
sum(case when b.mon=12 then b.yj else 0 end) as '十二月份',
from table 2 a left join table1 b on a.dep=b.dep group by a.dname
二.有一张成绩表,里面有3个字段:语文,数学,英语。请用一条sql语句查询出
这表里的记录并按以下条件显示出来:
大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文 数学 英语
及格 优秀 不及格
--------------------------------------------------------------------------
select
(case when 语文>=80 then '优秀'
when 语文>=60 then '及格'
else '不及格') as 语文,
(case when 数学>=80 then '优秀'
when 数学>=60 then '及格'
else '不及格') as 数学,
(case when 英语>=80 then '优秀'
when 英语>=60 then '及格'
else '不及格') as 英语,
from table
三.一张表内容如下:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果, 该如何写sql语句?
日期 胜 负
2005-05-09 2 2
2005-05-10 1 2
------------------------------------------
create table #tmp(
rq varchar(10),
shengfu nchar(1)
)
insert into #tmp values(' 2005-05-09 ','胜')
insert into #tmp values(' 2005-05-09 ','胜')
insert into #tmp values(' 2005-05-09 ','负')
insert into #tmp values(' 2005-05-09 ','负')
insert into #tmp values(' 2005-05-10 ','胜')
insert into #tmp values(' 2005-05-10 ','负')
insert into #tmp values(' 2005-05-10 ','负')
1)select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when shengfu='负' then 1 else 0 end)'负
' from #tmp group by rq
2) select N.rq,N.胜,M.负 from (
select rq,胜=count(*) from #tmp where shengfu='胜' group by rq)N inner join
(select rq,负=count(*) from #tmp where shengfu='负' group by rq)M on N.rq=M.rq
四.观众资料登记表
字段:编号,单位名称,姓名,手机.....
数据:.............................
题1:请查询姓名与手机重复的记录
题2:请查询一个单位名称有多个编号记录
题3:请查询一个单位名称只出来一条记录
比如有两条记录 编号 单位名称 姓名 手机
001 协广 张三 137
002 协广 李四 138
随便只出现其中任意一条即可
请用SQL语句写出来
要求:不是自动编号,允许重复,所有字段都是字符串,没有主建,字段值不唯一!
题1:请查询姓名与手机重复的记录
SELECT *
FROM 观众资料登记表 a
WHERE [姓名]+[手机] IN
(SELECT [姓名]+[手机]
FROM 观众资料登记表 b
GROUP BY [姓名],[手机]
HAVING COUNT(*)>1
)
题2:请查询一个单位名称有不同编号记录
SELECT *
FROM 观众资料登记表 a
WHERE [单位名称] IN
(SELECT [单位名称]
FROM 观众资料登记表 b
GROUP BY [单位名称]
HAVING COUNT(*)>1
)
题3:请查询一个单位名称只出来一条记录
SELECT DISTINCT [单位名称],
(SELECT TOP 1 [编号] FROM [观众资料登记表] b WHERE a.[单位名称] = b.[单位名称]) AS [编号],
(SELECT TOP 1 [姓名] FROM [观众资料登记表] b WHERE a.[单位名称] = b.[单位名称]) AS [姓名],
(SELECT TOP 1 [手机] FROM [观众资料登记表] b WHERE a.[单位名称] = b.[单位名称]) AS [手机]
FROM [观众资料登记表] a
五.学生表student,字段sID,sName,sAge;教师表teacher,字段tID,TName,tAge;教师-学生表ts,字段id,tID,sID。请统计每位教师有多少学生,要求教师年龄大于30,学生年龄大于12,并列出教师ID,教师姓名,学生人数
create table student(
sID int primary key,
sName varchar(50),
sAge int
)
insert into student values(1,'张三',9)
insert into student values(2,'张三2',14)
insert into student values(3,'张三3',33)
insert into student values(4,'张三4',11)
insert into student values(5,'张三5',19)
insert into student values(6,'张三6',25)
insert into student values(7,'张三7',27)
insert into student values(8,'张三8',23)
create table teacher(
tID int primary key,
tName varchar(50),
tAge int
)
insert into teacher values(1,'李明',29)
insert into teacher values(2,'李明2',34)
insert into teacher values(3,'李明3',23)
insert into teacher values(4,'李明4',31)
insert into teacher values(5,'李明5',19)
create table ts(
id int primary key,
sID int,
tID int
)
insert into ts values(1,1,2);
insert into ts values(2,2,3);
insert into ts values(3,3,2);
insert into ts values(4,4,3);
insert into ts values(5,5,4);
insert into ts values(6,6,2);
insert into ts values(7,7,4);
insert into ts values(8,8,2);
select t.tid,t.tname,count(*) from student s,teacher t,ts where s.sID = ts.sid and t.tid=ts.tid and s.sage>12 and t.tage>30 group by t.tid,t.tname