id  name  date
1    jay   20010101
2    jay   20020203
3    tony   20020203
4    tony  20030101
得到name字段比如jay 中 date 最大直所在的行的id.
结果
2 jay 20020203
4 tony 20030101

解决方案 »

  1.   

    select * from [Table] a where not exists(select 1 from [Table] where name=a.name and id>a.id)
      

  2.   

    select max(id) id,name,max(date) date
    from tb
    group by name
      

  3.   

    select * from [Table] t where id=(select max(id) from [Table] where name=t.name )
      

  4.   


    select * from 表 a where id =(select max(id) from 表 where a.name=name)
      

  5.   

    create table #test(id int,name varchar(20),date char(8))
    insert #test select 1 ,'jay','20010101'
    insert #test select 2 ,'jay','20020203'
    insert #test select 3 ,'tony','20020203'
    insert #test select 4 ,'tony','20030101'
    select * from #test a
    where not exists(select 1 from #test where a.name=name and cast(a.date as datetime)<cast(date as datetime))         id name                 date
    ----------- -------------------- --------
              2 jay                  20020203
              4 tony                 20030101(2 行受影响)
      

  6.   

    还可以:select * from [Table] t 
    where id=(select top 1 id from [Table] where name=t.name order by date desc)
      

  7.   


    max(id),这个我不需要啊。我只要求max(date).
    2个max,那ms sql,怎么判断?
      

  8.   

    还可以这样select id,name,date  from (
    select *,rn = row_number() over(partition by name order by date desc) from #test
    ) a
    where rn=1
             id name                 date
    ----------- -------------------- --------
              2 jay                  20020203
              4 tony                 20030101(2 行受影响)
      

  9.   

    谢谢.有没有其他效率好点的啊。
    not exists
    我这里会超时.因为数据量比较打.
      

  10.   


    我要的是date 最大的直.按照name分组.还要得到这行的其他数据比如 id.
      

  11.   

    create table #test(id int,name varchar(20),date char(8))
    insert #test select 1 ,'jay','20010101'
    insert #test select 2 ,'jay','20020203'
    insert #test select 3 ,'tony','20020203'
    insert #test select 4 ,'tony','20030101'select * from #test t where id=(select max(id) from #test where name=t.name )
    /*
             id name                 date
    ----------- -------------------- --------
              2 jay                  20020203
              4 tony                 20030101(2 行受影响)
    */用5楼的表数据.
      

  12.   

    id name date
    1 jay 20010101
    2 jay 20020203
    3 tony 20020203
    4 tony 20010101
    得到name字段比如jay 中 date 最大直所在的行的id等字段.
    结果
    2 jay 20020203
    3 tony 20020203
      

  13.   


    --------------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-04-02 12:31:32
    --  Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    --          Jul  9 2008 14:43:34 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
    --------------------------------------------------------------------------
    --> 生成测试数据表:tbIF NOT OBJECT_ID('[tb]') IS NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([id] NVARCHAR(10),[name] NVARCHAR(10),[date] DATETIME)
    INSERT [tb]
    SELECT '1','jay','20010101' UNION ALL
    SELECT '2','jay','20020203' UNION ALL
    SELECT '3','tony','20020203' UNION ALL
    SELECT '4','tony','20010101'
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    select * from [tb] t 
    where id=(select top 1 id from [tb] where name=t.name order by date desc)
    /*
    id         name       date
    ---------- ---------- -----------------------
    2          jay        2002-02-03 00:00:00.000
    3          tony       2002-02-03 00:00:00.000(2 行受影响)
    */
      

  14.   


    IF NOT OBJECT_ID('[tb]') IS NULL
        DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([id] NVARCHAR(10),[name] NVARCHAR(10),[date] DATETIME)
    INSERT [tb]
    SELECT '1','jay','20010101' UNION ALL
    SELECT '2','jay','20020203' UNION ALL
    SELECT '3','tony','20020203' UNION ALL
    SELECT '4','tony','20030101'
    GO
    SELECT tb.* FROM tb INNER JOIN
    (
    SELECT NAME,MAX(date) AS date FROM tb GROUP BY [name]
    ) a
    ON tb.[name]=a.[NAME] AND tb.date=a.date
    ORDER BY id ASC
    (4 行受影响)
    id         name       date
    ---------- ---------- -----------------------
    2          jay        2002-02-03 00:00:00.000
    4          tony       2003-01-01 00:00:00.000(2 行受影响)
      

  15.   

    select id from [table1] where data=(select max(date)from [tale1] group by [Name])
    这个中Group By,没有太大的意义!
      

  16.   

    create table #test(id int,name varchar(20),date char(8))
    insert #test select 1 ,'jay','20010101'
    insert #test select 2 ,'jay','20020203'
    insert #test select 3 ,'tony','20020203'
    insert #test select 4 ,'tony','20030101'select * from #test a where date=(
    select  max(b.date) from #test b where b.name=a.name)id          name                 date     
    ----------- -------------------- -------- 
    4           tony                 20030101
    2           jay                  20020203(所影响的行数为 2 行)
      

  17.   


    --基本速度快的方法都出来了
    --据说这种最快
    select 
        * 
    from 
        your_table a 
    where 
        date=(select max(date) from your_table where name=a.name)--不过该加的索引还是得加上
      

  18.   

    max(id)   有错。在此题发现不了。画蛇添足。如果1 变成 8 ,答案是什么?id name date
    8 jay 20010101
    2 jay 20020203
    3 tony 20020203
    4 tony 20010101
    得到name字段比如jay 中 date 最大直所在的行的id等字段.
    结果
    8 jay 20020203
    3 tony 20020203
      

  19.   

     group by 为分组指令,在进行选择记录的时候或是在执行别的操作中,可以将你选择的类型不同进行分组。
      

  20.   

    group by 不就是分组函数
      

  21.   

    GROUP BY 是分组查询, 一般 GROUP BY 是和 聚合函数配合使用,你可以想想你用了GROUP BY 按  ITEM.ITEMNUM 这个字段分组,那其他字段内容不同,变成一对多又改如何显示呢,比如下面所示A  B
    1  abc
    1  bcd
    1  asdfgselect A,B from table group by A
    你说这样查出来是什么结果,A  B
        abc 
    1  bcd
        asdfg右边3条如何变成一条,所以需要用到聚合函数,比如select A,count(B) 数量 from table group by A
    这样的结果就是
    A  数量
    1   3group by 有一个原则,就是 select 后面的所有列中,没有使用聚合函数的列,必须出现在 group by 后面