请问如何查询倒数第2、3、4条记录CatId    CatName    AddedDate
--------------------------------
1        Bird       2008-1-1
      (省略数万行)
17878    Sheep      2009-9-13
17879    Pig        2009-9-14
17880    Fish       2009-9-15
17881    Dog        2009-9-15        得到
CatId    CatName    AddedDate
--------------------------------
17880    Fish       2009-9-15
17879    Pig        2009-9-14
17878    Sheep      2009-9-13

解决方案 »

  1.   

    select top 3 CatId,CatName,AddedDate from 表 order  by catid desc
      

  2.   

    select top 3 * from (select top 4 * from tb order by catid desc)t order by catid
      

  3.   

    取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 id between n to m
      

  4.   

    SELECT TOP 3 CatId,CatName,AddedDate FROM 
    (SELECT TOP 4 CatId,CatName,AddedDate FROM TB ORDER BY CatId DESC)AS T ORDER BY CatId 
      

  5.   

    select top 3 CatId,CatName,AddedDate from 表 where CatId<>(select max(CatId) from 表 ) order  by catid desc
      

  6.   

    select top 3 * from (select top 4 * from tb order by catid desc)t order by catid
      

  7.   

    SELECT * FROM 
    (
    SELECT TOP 3 CatId,CatName,AddedDate FROM 
    (SELECT TOP 4 CatId,CatName,AddedDate FROM TB ORDER BY CatId DESC)AS T ORDER BY CatId )AS T ORDER BY CatId  DESC
      

  8.   

    不好意思大家们!问题我提错了!应该是:如果是当前日期,就查询倒数3条,如果小于当前日期,就查询倒数2、3、4条。
    (最好不要用if else,因为我这个查询是要放在另一个子查询中,不能转换成字符串的形式连接变量,因为写的太复杂了不好看,如果不写成字符串的形式就可以用if else)
      

  9.   

    不好意思大家们!问题我提错了! 应该是:如果是当前日期,就查询倒数2、3、4条,如果小于当前日期,就查询倒数3条。 
    (最好不要用if else,因为我这个查询是要放在另一个子查询中,不能转换成字符串的形式连接变量,因为写的太复杂了不好看,如果不写成字符串的形式就可以用if else)
      

  10.   

    该表的数据分为两种类型,一种是一天增加几条,一种是几天才增加一条。暂且叫做类型A、B。如果是 A 类数据:如果最后一个日期等于当前日期,就查询前一天的3条数据,假如前一天只有2条数据,就查询前2天的数据,以此类推,反正是要查询出小于当前日期的3条数据。如果是 B 类数据:如果最后一个日期等于当前日期,就查询倒数2、3、4条数据,如果最后一个日期小于当前日期,就查询倒数3条数据。综上所述,意思就是,不能查询当前日期的数据,只能查询小于当前日期三条数据。
      

  11.   

    ---try
    declare @table table(catid int,catname nvarchar(10),addedate datetime)
    insert into @table select 1,'bird','2008-1-1'
             union all select 17878,'sheep','2009-9-13'
             union all select 17879,'pig','2009-9-14'
             union all select 17880,'fish','2009-9-15'
             union all select 17881,'dog','2009-9-15'
             
    select top 4 *,DATEDIFF(DD,addedate,GETDATE())时差 into # from @table 
                         order by addedate desc
    if exists (select * from # a where exists 
        (select 1 from # b where a.catname=b.catname group by b.addedate having COUNT(*)>2))
      select top 3 * from # 
    else 
      select top 3 catid,catname,addedate from # order by addedate 
    go                 
    drop table #
    catid       catname    addedate
    ----------- ---------- -----------------------
    17878       sheep      2009-09-13 00:00:00.000
    17879       pig        2009-09-14 00:00:00.000
    17880       fish       2009-09-15 00:00:00.000(3 行受影响)
      

  12.   


    select top 3 * 
    from tb
    where datediff(day,AddedDate,getdate())>0
    order by AddedDate desc