(1). 表结构如下:(表名Table1)
自动编号(SID) 学号(SNo) 姓名(SName) 课程编号(CNo) 课程名称(CName) 分数(Score)
1 01030230 张三 0001 数学 90
2 01030231 李四 0001 数学 89
3 01030232 张三 0001 物理 62
5 ……
A、 查询数学分数在表中第10名到第15名的学生信息
B、 用一条SQL语句 查询出每门课都大于85分的学生姓名
C、用SQL语句得到如下表结果:
姓名 平均分
张三 85
李四 80
……
(2). 转化 字符串 ‘2008012015:12:34’ 为 字符串 ‘2008-01-20 15:12:34’。[注意第一个字符串中间无空格]
(3) 有二元一次方程式: 4X+3.5Y=15( X、Y均为自然数),求得Y的最大值。写出实现过程。(4) 列出未使用索引的可能原因(四种以上的)谢谢了
自动编号(SID) 学号(SNo) 姓名(SName) 课程编号(CNo) 课程名称(CName) 分数(Score)
1 01030230 张三 0001 数学 90
2 01030231 李四 0001 数学 89
3 01030232 张三 0001 物理 62
5 ……
A、 查询数学分数在表中第10名到第15名的学生信息
B、 用一条SQL语句 查询出每门课都大于85分的学生姓名
C、用SQL语句得到如下表结果:
姓名 平均分
张三 85
李四 80
……
(2). 转化 字符串 ‘2008012015:12:34’ 为 字符串 ‘2008-01-20 15:12:34’。[注意第一个字符串中间无空格]
(3) 有二元一次方程式: 4X+3.5Y=15( X、Y均为自然数),求得Y的最大值。写出实现过程。(4) 列出未使用索引的可能原因(四种以上的)谢谢了
取n到m行1.
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/) 2.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表
set rowcount n --只取n条结果
select * from 表变量 order by columnname desc 3.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc
4.如果tablename里没有其他identity列,那么:
先生成一个序列,存储在一临时表中.
select identity(int) id0,* into #temp from tablename 取n到m条的语句为:
select * from #temp where id0 > =n and id0 <= m 如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
exec sp_dboption 你的DB名字,'select into/bulkcopy',true
5.如果表里有identity属性,那么简单:
select * from tablename where identity_col between n and m 6.SQL2005开始.可以使用row_number() over()生成行号
;with cte as
(
select id0=row_number() over(order by id),* from tablename
)
select * from cte where id0 between n to m
FROM tb AS A
WHERE NOT EXISTS(
SELECT *
FROM tb
WHERE SNO=A.SNO
AND Score<85
)
Name,
AVG(Score) AS 平均分
FROM tb
GROUP BY Name
/*
-----------------
20080120 15:12:34(1 行受影响)*/
/*
-----------------------
2008-01-20 15:12:34.000(1 行受影响)
*/
select * from
(
select t.* , px = (select count(Score) from table1 where Score > t.Score) + 1 from table1 t
) m
where px between 10 and 15--同分合并名次
select * from
(
select t.* , px = (select count(distinct Score) from table1 where Score > t.Score) + 1 from table1 t
) m
where px between 10 and 15B、 用一条SQL语句 查询出每门课都大于85分的学生姓名 select * from table1 where SNo not in (select distinct SNo from table1 where Score <= 85)
C、用SQL语句得到如下表结果:
姓名 平均分
张三 85
李四 80 select SName 姓名 , cast(avg(Score*1.0) as decimal(18,1)) 分数 from table1 group by SName
(2). 转化 字符串 ‘2008012015:12:34’ 为 字符串 ‘2008-01-20 15:12:34’。[注意第一个字符串中间无空格]
select left('2008012015:12:34',4) + '-' +
substring('2008012015:12:34',5,2) + '-' +
substring('2008012015:12:34',7,2) + ' ' +
right('2008012015:12:34',8)
/*
-----------------------------------
2008-01-20 15:12:34(所影响的行数为 1 行)
*/
select * from
(
select * row_number()over(partition by 课程名称 order by 分数 DESC) rank1 from 表
) A
where A.课程名称='数学' and rank1 between 10 and 15B.
select 姓名 from #T group by 姓名 having MIN(分数)>85 C.
select 姓名,SUM(分数)/COUNT(姓名) from #T group by 姓名
substring(col , pos1 , pos2)
left(col , pos)
right(col ,pos)
declare @result float
declare @i int
set @i=0
while @i<=15
begin
set @result=15-4*@i
set @result=@result/3.5
set @t=cast(@result as int)
IF cast(@t as float)=@result
begin
Print @result
break
end
set @i+=1
end
A
select top 6 * from ( SELECT TOP 15 * from table1 order by Score desc ) a order by a.Score asc
b
select SName from table1 where SName not in(select SName from table1 where Score<85 )
c
SELECT Name, AVG(Score) AS 平均分 FROM table1 GROUP BY Name
(2)
SELECT CONVERT(DATETIME,STUFF('2008012015:12:34',9,0,' '),120)
insert into @表 select 1,'01030230','张三','0001','数学',90
union all select 2,'01030231','李四','0001','数学',89
union all select 3,'01030232','张三','0001','物理',62
1.select * from
(select DENSE_RANK() over(order by 分数) 名次,* from @表) b
where 名称
between 10 and 15
2.select * from @表 where 分数>85
3.declare @str nvarchar(20)
set @str='2008012015:12:34'
--select SUBSTRING(@str,5,6)
select LEFT(@str,4)+'-'+SUBSTRING(@str,5,2)+ '-' + SUBSTRING(@str,7,2)+' '+ RIGHT(@str,8)
declare @t int
declare @i int
declare @result float
set @i=1
while @i<=15
begin
set @result =(15-4*@i)/3.5
if(@result=cast(@result as int))
begin
print @result
break
end
set @i=@i+1
end