我现在有如下结构的一个表
ID 卷号 期号 时间
1 一 一 12:10
2 一 二 12:40
3 二 一 11:10
4 二 二 10:10
我要取出的数据要求按"时间"倒序排列,但是每个卷号只取一条记录。
也就是说按照上面的数据只提取ID是2,3的数据,请问这样的SQL语句应该怎么写?

解决方案 »

  1.   

    --方法一
    Select A.* From 表 A 
    Inner Join
    (Select 卷号, Max(时间) As 时间 From 表 Group By 卷号) B
    On A.卷号 = B.卷号 And A.时间 = B.时间--方法二
    Select * From 表 A Where Not Exists(Select 时间 From 表 Where 卷号 = A.卷号 And 时间 > A.时间)--方法三
    Select * From 表 A Where 时间 In (Select Max(时间) From 表 Group By 卷号)
      

  2.   

    select *
    from 表 a 
    where exists(select 1 from 表 where a.卷号=卷号 and a.期号=期号 and a.时间>时间)
      

  3.   

    select max(时间),卷号 from 表 group by 卷号
      

  4.   

    我现在有如下结构的一个表
    ID 卷号 期号 时间
    1 一 一 12:10
    2 一 二 12:40
    3 二 一 11:10
    4 二 二 10:10
    我要取出的数据要求按"时间"倒序排列,但是每个卷号只取一条记录。
    也就是说按照上面的数据只提取ID是2,3的数据,请问这样的SQL语句应该怎么写?select a.* from tb a,
    (select 卷号 ,max(时间) 时间 from tb group 卷号) b
    where a.时间 = b.时间 and a.卷号 = b.卷号
    order by a.时间 desc
      

  5.   

    create table tab(ID int,卷号 varchar(10),期号 varchar(10),时间 varchar(10))
    insert tab
    select 1,'一','一','12:10'
    union select 2,'一','二','12:40'
    union select 3,'二','一','11:10'
    union select 4,'二','二','10:10'
    select *
    from tab a 
    where exists(select 1 from tab where a.卷号=卷号 and a.时间>时间)
    drop table tab
      

  6.   

    先按时间dese,然后select distinct 卷号
      

  7.   

    /**ID          卷号         期号         时间         
    ----------- ---------- ---------- ---------- 
    2           一          二          12:40
    3           二          一          11:10(2 row(s) affected)**/
      

  8.   

    --建立测试环境
    create table #tb(id int,卷号 varchar(10),期号 varchar(10),时间 varchar(10))
    insert #tb(id,卷号,期号,时间)
    select '1','一','一','12:10' union all
    select '2','一','二','12:40' union all
    select '3','二','一','11:10' union all
    select '4','二','二','10:10'
    go
    --执行测试语句
    select * from #tb a where not exists(select 时间 from #tb where 卷号 = a.卷号 and 时间 > a.时间)
    go
    --删除测试环境
    drop table #tb
    go
    /*--测试结果
    id          卷号         期号         时间         
    ----------- ---------- ---------- ---------- 
    2           一          二          12:40
    3           二          一          11:10(2 row(s) affected)
    */
      

  9.   

    leo_lesley(leo) ( ) 信誉:100  2007-08-15 16:30:42  得分: 0  
     
    select *
    from tab a 
    where exists(select 1 from tab where a.卷号=卷号 and a.时间>时间)----------
    掉了not
      

  10.   

    --創建測試環境
    Create Table 表(ID Int, 卷号 Varchar(10), 期号 Varchar(10), 时间 Varchar(10))
    Insert 表 Select 1, '一', '一', '12:10'
    Union Select 2, '一', '二', '12:40'
    Union Select 3, '二', '一', '11:10'
    Union Select 4, '二', '二', '10:10'
    GO
    --測試
    --方法一
    Select A.* From 表 A 
    Inner Join
    (Select 卷号, Max(时间) As 时间 From 表 Group By 卷号) B
    On A.卷号 = B.卷号 And A.时间 = B.时间
    Order By A.卷号, A.时间--方法二
    Select * From 表 A Where Not Exists(Select 时间 From 表 Where 卷号 = A.卷号 And 时间 > A.时间)
    Order By A.卷号, A.时间--方法三
    Select * From 表 A Where 时间 In (Select Max(时间) From 表 Group By 卷号)
    Order By A.卷号, A.时间
    GO
    --刪除測試環境
    Drop Table 表
    --結果
    /*
    ID 卷号 期号 时间
    2 一 二 12:40
    3 二 一 11:10
    */
      

  11.   

    leo_lesley(leo) ( ) 信誉:100  2007-08-15 16:30:42  得分: 0  
     
    select *
    from tab a 
    where exists(select 1 from tab where a.卷号=卷号 and a.时间>时间)----------
    掉了not
    ---------------------这个没有错阿~~~    你运行一下就知道结果了, 我的语句和你的不一样的
      

  12.   

    Select * From 表 A Where Not Exists(Select 时间 From 表 Where 卷号=A.卷号 And 时间 〉A.时间)--------------------------select * from tab a where exists(select 1 from tab where a.卷号=卷号 and a.时间>时间)-----你的是时间>A.时间   我的是  a.时间>时间,明显不一样。
      

  13.   

    leo_lesley(leo) ( ) 信誉:100  2007-8-15 16:41:45  得分: 0  
     
     
       
    Select * From 表 A Where Not Exists(Select 时间 From 表 Where 卷号=A.卷号 And 时间 〉A.时间)--------------------------select * from tab a where exists(select 1 from tab where a.卷号=卷号 and a.时间>时间)-----你的是时间>A.时间   我的是  a.时间>时间,明显不一样。  
    ---------------邏輯錯誤的。你加兩條數據測試看看。