有一个数据我想用sql语句得到下面结果。
请大家帮吗看看
想要的结果是所有编号中最近的2行
 

解决方案 »

  1.   

    select top 2 * from t order by 日期 desc;
      

  2.   

    请把帖子移到sql基础类,谢谢
    select * from 
    (select row_number() over(partition by 编号 order by cast(日期 as datetime) no,*) from tb)
    where no<3
      

  3.   

    select * from tb t where (select count(1) from tb where 编号=t.编号 and date>t.date)<2 
      

  4.   

    select * from tablet t where not exists(
    select 1 from tablet where t.id=id and t.日期<日期
    )
      

  5.   

    这张图好了;with cte as
    (
    select * ,rn=row_number() over(partition by 编号 order by 日期 desc)
    )
    select * from cte whre rn<=2
      

  6.   


    use tempdb;
    /*
    create table t3
    (
    编号 int not null,
    日期 nvarchar(10) not null
    );
    insert into t3(编号,日期)
    values
    (1,'20110101'),
    (1,'20110102'),
    (2,'20110103'),
    (2,'20110104'),
    (2,'20110105'),
    (1,'20110106'),
    (3,'20110107'),
    (3,'20110108'),
    (2,'20110109'),
    (3,'20110110'),
    (4,'20110111');
    */
    select t.编号,t.日期
    from
    (
    select 编号,ROW_NUMBER() over(partition by 编号 order by 日期 desc) as [sortnum],日期
    from t3
    ) as t
    where t.sortnum < 3;
      

  7.   

    'ROW_NUMBER' is not a recognized function name.
    我用的数据库是mssql2000这里好像不支持ROW_NUMBE