sqlserver 统计sql语句大全收藏_MsSql

1.计算每个人的总成绩并排名

select name,sum(score) as allscore from stuscore group by name order by allscore

2.计算每个人的总成绩并排名

select distinct t1.name,t1.stuid,t2.allscore from stuscore t1,( select stuid,sum(score) as allscore from stuscore group by stuid)t2where t1.stuid=t2.stuidorder by t2.allscore desc

3. 计算每个人单科的最高成绩

select t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,(select stuid,max(score) as maxscore from stuscore group by stuid) t2where t1.stuid=t2.stuid and t1.score=t2.maxscore

4.计算每个人的平均成绩

select distinct t1.stuid,t1.name,t2.avgscore from stuscore t1,(select stuid,avg(score) as avgscore from stuscore group by stuid) t2where t1.stuid=t2.stuid

5.列出各门课程成绩最好的学生

select t1.stuid,t1.name,t1.subject,t2.maxscore from stuscore t1,(select subject,max(score) as maxscore from stuscore group by subject) t2where t1.subject=t2.subject and t1.score=t2.maxscore
6.列出各门课程成绩最好的两位学生

select distinct t1.* from stuscore t1 where t1.id in (select top 2 stuscore.id from stuscore where subject = t1.subject order by score desc) order by t1.subject

7.学号 姓名 语文 数学 英语 总分 平均分

select stuid as 学号,name as 姓名,sum(case when subject='语文' then score else 0 end) as 语文,sum(case when subject='数学' then score else 0 end) as 数学,sum(case when subject='英语' then score else 0 end) as 英语,sum(score) as 总分,(sum(score)/count(*)) as 平均分from stuscoregroup by stuid,name order by 总分desc

8.列出各门课程的平均成绩

select subject,avg(score) as avgscore from stuscoregroup by subject

9.列出数学成绩的排名

declare @tmp table(pm int,name varchar(50),score int,stuid int)insert into @tmp select null,name,score,stuid from stuscore where subject='数学' order by score descdeclare @id intset @id=0;update @tmp set @id=@id+1,pm=@idselect * from @tmp

select DENSE_RANK () OVER(order by score desc) as row,name,subject,score,stuid from stuscore where subject='数学'order by score desc

10. 列出数学成绩在2-3名的学生

select t3.* from(select top 2 t2.* from (select top 3 name,subject,score,stuid from stuscore where subject='数学'order by score desc) t2 order by t2.score) t3 order by t3.score desc

11. 求出李四的数学成绩的排名

declare @tmp table(pm int,name varchar(50),score int,stuid int)insert into @tmp select null,name,score,stuid from stuscore where subject='数学' order by score descdeclare @id intset @id=0;update @tmp set @id=@id+1,pm=@idselect * from @tmp where name='李四'

12. 课程 不及格(-59) 良(-80) 优(-100)

select subject, (select count(*) from stuscore where score<60 and subject=t1.subject) as 不及格,(select count(*) from stuscore where score between 60 and 80 and subject=t1.subject) as 良,(select count(*) from stuscore where score >80 and subject=t1.subject) as 优from stuscore t1 group by subject

13. 数学:张三(50分),李四(90分),王五(90分),赵六(76分)

declare @s varchar(1000)set @s=''select @s =@s+','+name+'('+convert(varchar(10),score)+'分)' from stuscore where subject='数学' set @s=stuff(@s,1,1,'')print '数学:'+@s

时间: 2024-08-02 09:54:23

sqlserver 统计sql语句大全收藏_MsSql的相关文章

sqlserver 统计sql语句大全收藏

1.计算每个人的总成绩并排名 select name,sum(score) as allscore from stuscore group by name order by allscore 2.计算每个人的总成绩并排名 select distinct t1.name,t1.stuid,t2.allscore from stuscore t1,( select stuid,sum(score) as allscore from stuscore group by stuid)t2where t1

SQL Sever查询语句大全集锦_MsSql

一. 简单查询 简单的Transact-SQL查询只包括选择列表.FROM子句和WHERE子句.它们分别说明所查询列.查询的 表或视图.以及搜索条件等. 例如,下面的语句查询testtable表中姓名为"张三"的nickname字段和email字段. 代码:SELECT `nickname`,`email`FROM `testtable`WHERE `name`='张三' (一) 选择列表 选择列表(select_list)指出所查询列,它可以是一组列名列表.星号.表达式.变量(包括局

经典50个SQL语句大全

  50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 问题: 1.查询"001"课程比"002"课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC wh

操作日期的SQL语句大全

  操作日期的SQL语句大全: 1.显示本月第一天 SELECT DATEADD(mm,DATEDIFF(mm,0,getdate()),0) select convert(datetime,convert(varchar(8),getdate(), 120)+'01',120) 2.显示本月最后一天 select dateadd(day,-1,convert(datetime,convert (varchar(8),dateadd(month,1,getdate()),120)+'01',12

sqlserver的sql语句转成oracle

问题描述 sqlserver的sql语句转成oracle SELECT FF.*, AF.FUNCGROUPNAME FROM (SELECT F.FUNCCODE,F.FUNCNAME,F.ISMENU,F.FUNCGROUPID, (CASE WHEN PARTY_ID IS NULL THEN 0 ELSE 1 END) ISCHECK FROM APP_FUNCTION F LEFT JOIN CAP_RESAUTH CR ON F.FUNCCODE = CR.RES_ID AND CR

Mssql,Access的sql经典SQL语句大全_数据库其它

下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL-数据定义语言(CREATE,ALTER,DROP,DECLARE) DML-数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL-数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1.说明:创建 数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份

Mssql,Access的sql经典SQL语句大全

常用不常用的一些sql语句,对数据库操作不是很熟练的朋友可以查询.  下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL-数据定义语言(CREATE,ALTER,DROP,DECLARE) DML-数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL-数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1.说明:创建 数据库 CREATE DATABASE database-name 2.说明

经典SQL语句大全第1/2页_MsSql

下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL-数据定义语言(CREATE,ALTER,DROP,DECLARE) DML-数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL-数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 一.基础 1.说明:创建数据库 复制代码 代码如下: CREATE DATABASE database-name 2.说明:删除数据库 复制代码 代码如下: drop database d

经典sql语句大全

一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:mssql7backupMyNwind_1.dat' --- 开始 备份 BACKUP DATABASE pubs TO testBack 4.说明:创建新表