比较常用的sql 查询语句
来源:互联网 发布:java做验证码识别训练 编辑:程序博客网 时间:2024/06/05 17:26
create table SC
(
SNo int,
Sname nvarchar(50),
Cname nvarchar(50),
Score int,
)
insert into SC values
(1 , '张三', '数学', 89),
(1 ,'张三', '语文' ,80),
(1, '张三', '英语', 70),
(2, '李四', '数学', 90),
(2, '李四', '语文', 70),
(2, '李四', '英语', 80)
-- 计算每个人的总成绩并排名(要求显示字段:姓名,总成绩)
select Sname,SUM(Score)as SumScore from SC group by Sname order by SumScore
--计算每个人的总成绩并排名
select distinct t1.Sname, t1.SNo, t2.SumScore from SC t1,
(
select SNo, SUM(Score) as SumScore from SC group by SNo)t2
where t1.SNo = t2.SNo
--计算每个人单科的最高成绩
select t1.Sname,t1.Cname, t2.MaxScore from SC t1,
(select Sname, MAX(Score)as MaxScore from SC group by Sname)t2
where t1.Sname = t2.Sname and t1.Score = t2.MaxScore
-- 计算每个人的平均成绩(要求显示字段: 学号,姓名,平均成绩)
select distinct t1.Sno, t1.Sname, t2.AvgScore from SC t1,
(select Sno,AVG(Score)as AvgScore from SC group by SNo)t2
where t1.SNo = t2.SNo
-- 列出各门课程成绩最好的学生(要求显示字段: 学号,姓名,科目,成绩)
select t1.Sno,t1.Sname,t2.Cname,t2.MaxScore from SC t1,
(select Cname, MAX(Score) as MaxScore from SC group by Cname)t2
where t1.Cname = t2.Cname and t2.MaxScore = t1.Score
--列出各门课程成绩最好的两位学生(要求显示字段: 学号,姓名,科目,成绩)
select distinct t1.* from SC t1 where t1.SNo in
(select top 2 SNo from SC where t1.Cname = SC.Cname order by Score desc)
order by t1.Cname
-- 统计如下:学号姓名语文 数学 英语总分平均分
select sno,Sname,
SUM(case when cname = '语文' then score else 0 end) as 语文,
SUM(case when cname = '数学' then score else 0 end) as 数学,
SUM(case when cname = '英语' then score else 0 end) as 英语,
SUM(Score)as SumScore, AVG(score) as AvgScore
from SC
group by SNo,Sname
order by SumScore desc
-- 列出各门课程的平均成绩(要求显示字段:课程,平均成绩)
select cname, AVG(score)as AvgSum from SC group by cname
--列出数学成绩的排名(要求显示字段:学号,姓名,成绩,排名)
declare @temp table(Sno int, Sname nvarchar(50), Score int, Sort int)
insert @temp select sno,sname,score,null from SC where SC.Cname='数学' order by Score desc
declare @id int
set @id=0;
update @temp set @id=@id +1, Sort = @id
select * from @temp
-- 列出数学成绩在-3名的学生(要求显示字段:学号,姓名,科目,成绩)
select top 2 t1.* from
(select top 3 * from sc where Cname='数学' order by Score)t1 order by t1.Score desc
-- 求出李四的数学成绩的排名
declare @temp1 table(Sno int, Sname nvarchar(50), Score int, Sort int)
insert @temp1 select sno,sname,score,null from SC where SC.Cname='数学' order by Score desc
declare @id1 int
set @id1=0;
update @temp1 set @id1=@id1 +1, Sort = @id1
select * from @temp1 where Sname = '李四'
-- 统计如下:课程不及格(-59)个 良(-80)个 优(-100)个
select cname,
(select COUNT(*) from SC where Score < 60 and Cname = t1.cname) as 不及格,
(select COUNT(*) from SC where Score between 60 and 80 and Cname = t1.cname) as 良,
(select COUNT(*) from SC where Score between 60 and 80 and Cname = t1.cname) as 优
from SC t1
group by t1.Cname
--统计如下:数学:张三(50分),李四(90分),王五(90分),赵六(76分)
declare @s varchar(1000)
set @s=''
select @s =@s+','+Sname+'('+convert(varchar(10),Score)+'分)' from sc where Cname='数学'
set @s=stuff(@s,1,1,'')
print '数学:'+@s
- 比较常用的sql 查询语句
- 比较常用的sql语句
- 常用的SQL查询语句
- 常用的SQL查询语句
- 常用的sql查询语句
- 常用的sql查询语句
- 一些比较难的SQL语句查询
- sql server日期比较日期查询常用语句
- sql server日期比较、日期查询常用语句
- sql server日期比较、日期查询常用语句
- sql server日期比较、日期查询常用语句
- sql server日期比较、日期查询常用语句
- sql server日期比较、日期查询常用语句(转)
- SQL 日期截取? & 日期比较&日期查询常用语句
- sql server日期比较日期查询常用语句
- 比较常用的SQL语句语法(Oracle)
- 比较常用的SQL语句语法(Oracle)
- SQL常用查询语句
- MDI的子窗体一旦最大化后,子窗体的图标就越过菜单栏跑到最上面去了(已解决)
- 最近做项目中用到的文字或图片水平和垂直方向平滑滚动(含Ajax效果)
- 分析器错误消息: 未能加载文件或程序集“Microsoft.Office.Interop....
- oracle中imp命令详解[转]
- UIComponent研究
- 比较常用的sql 查询语句
- C++标准ISO-IEC-14882-2003:第14章:模板-第2节:模板特化的名字
- Effective C++笔记: 资源管理(一)
- 利用Java的Properties 类读取配置文件信息
- DM9000
- SystemC的语言结构简介
- event在IE和FF中使用时的兼容问题
- 聚集索引与非聚集索引的适用情况
- flex编写标准