写个查询语句怎么写都不对
打个比方吧 表名为 table1
表中有如下数值
num  type val
1    0    1
1    1    2
1    0    4
1    0    5
2    0    4
2    0    7
2    0    1
2    1    4
2    0    2
2    1    1我想取的表中 按num和type来分类按val来降序排序的前2名值 想要的结果如下num  type val
1    0    5
1    0    4
1    1    2
2    0    7
2    0    4
2    1    4
2    1    4请问sql语句怎么写。。不甚感激

解决方案 »

  1.   

    一条语句搞定啊  前两条的意思就是说按val降序的前两条
      

  2.   

    select * from table1  group by num desc ,type desc
      

  3.   

    select * from table1  group by num desc ,val desc
      

  4.   

    select distinct num,type,val
    from 表 AS A where val in(
    select top 2 val from 表 where num=A.num and type= A.type
    order by val desc)order by num,type, val desc
      

  5.   

    你这语句直接报 关键字 'desc' 附近有语法错误。
      

  6.   

    select Gpdm,Sc,Issuedate from [210104000] where Issuedate in(select top 2 Issuedate from [210104000]  order by Issuedate desc)order by Gpdm,Sc,Issuedate desc
    这是我仿照你写的代码 选出的结果只有两条 我的意思是说呢  把所有数据 按num  type分类(比如说 1 0是一类 1  1 是一类 ) 对每个分类中的数据按降序排序 选出前两名(只要前两名的数据 ) 表达不清楚的地方还望见谅
      

  7.   


     select Gpdm,Sc,Issuedate from [210104000] A where Issuedate in(select top 2 Issuedate from [210104000] WHERE  Gpdm = A.Gpdm AND Sc = A.Sc order by Issuedate desc)order by Gpdm,Sc,Issuedate desc
      

  8.   

    额。。少了个..A 结果出来了  讲下这语句sql 是怎么执行的内
      

  9.   

    create table table1(num int ,type int ,val int)
    go 
    insert into table1 
    select 1,0,1 union all
    select 1,1,2 union all
    select 1,0,4 union all
    select 1,0,5 union all
    select 2,0,4 union all
    select 2,0,7 union all
    select 2,0,1 union all
    select 2,1,4 union all
    select 2,0,2 union all
    select 2,1,1 select distinct num,type,val 
    from table1 AS A where val in( 
    select top 2 val from  table1  where num=A.num and type= A.type 
    order by val desc) order by num,type, val desc
    结果:1 0 5
    1 0 4
    1 1 2
    2 0 7
    2 0 4
    2 1 4
    2 1 1
      

  10.   

    呵呵 是我问题。。sorry。那结果出来了。我把A以为是不同表的关联了。。