(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) 列出未使用索引的可能原因(四种以上的)谢谢了

解决方案 »

  1.   

    1.
    取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
      

  2.   

    (3) 有二元一次方程式: 4X+3.5Y=15( X、Y均为自然数),求得Y的最大值。写出实现过程。 作业题吧?
      

  3.   

    2.SELECT *
    FROM tb AS A
    WHERE NOT EXISTS(
              SELECT *
              FROM tb
              WHERE SNO=A.SNO
                 AND Score<85
          )
      

  4.   

    3.SELECT
        Name,
        AVG(Score) AS 平均分
    FROM tb
    GROUP BY Name
      

  5.   

    SELECT STUFF('2008012015:12:34',8,0,' ')
      

  6.   

    SELECT STUFF('2008012015:12:34',9,0,' ')
    /*
    -----------------
    20080120 15:12:34(1 行受影响)*/
      

  7.   

    SELECT CONVERT(DATETIME,STUFF('2008012015:12:34',9,0,' '),120) 
    /*
    -----------------------
    2008-01-20 15:12:34.000(1 行受影响)
    */
      

  8.   

    A、 查询数学分数在表中第10名到第15名的学生信息  --同分不合并名次
    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 行)
    */
      

  9.   

    A.
    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 姓名
      

  10.   

    (3) 有二元一次方程式: 4X+3.5Y=15( X、Y均为自然数),求得Y的最大值。写出实现过程。y = (15 - 4x) / 3.5按条件 X、Y均为自然数,所以(15 - 4x)必须为3.5的整数倍.15 - 4x = 7 / 10.5 / 13.54x = 8 / 4.5 / 1.5所以y = 2
      

  11.   

    (4) 列出未使用索引的可能原因(四种以上的) 使用 like '%aa%' 
         substring(col , pos1 , pos2)
         left(col , pos)
         right(col ,pos)
      

  12.   

    3.declare @t int
    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
      

  13.   


    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) 
      

  14.   

    declare @表 table (自动编号 int,学号 nvarchar(10),姓名 nvarchar(10),课程编号 int,课程名称 nvarchar(10),分数 int)
    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)
      

  15.   

    3.
    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
      

  16.   

    SELECT SName as '姓名', AVG(Score) AS '平均分' FROM Table1 GROUP BY SName