表结构如下
员工编号 类型     时间
  1       a     2009-3-1
  1       a     2009-3-10
  1       b     2009-4-1
  1       b     2009-4-10
  2       a     2009-1-15
  2       a     2009-1-1按照员工,类型分组 选出最大的时间 
结果如下:员工编号 类型       时间
   1      a      2009-3-10
   1      b      2009-4-10
   2      a      2009-1-15

解决方案 »

  1.   

    select 员工编号,类型,max(时间) as 时间
    from tb
    group by 员工编号,类型
      

  2.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([员工编号] int,[类型] varchar(1),[时间] datetime)
    insert [tb]
    select 1,'a','2009-3-1' union all
    select 1,'a','2009-3-10' union all
    select 1,'b','2009-4-1' union all
    select 1,'b','2009-4-10' union all
    select 2,'a','2009-1-15' union all
    select 2,'a','2009-1-1'
     
    ---查询---
    select 员工编号,类型,max(时间) as 时间
    from tb
    group by 员工编号,类型
    order by 员工编号,类型
    ---结果---
    员工编号        类型   时间                                                     
    ----------- ---- ------------------------------------------------------ 
    1           a    2009-03-10 00:00:00.000
    1           b    2009-04-10 00:00:00.000
    2           a    2009-01-15 00:00:00.000(所影响的行数为 3 行)
      

  3.   

    select 员工编号,类型,max(时间) as 时间 from tb group by 员工编号,类型
      

  4.   

    不好意思 漏了一个字段表结构如下 
    员工编号 类型   货物名称      时间 
      1      a     A货    2009-3-1 
      1      a     B货    2009-3-10 
      1      b     c货    2009-4-1 
      1      b     d货    2009-4-10 
      2      a     e货    2009-1-15 
      2      a     f货    2009-1-1 按照员工,类型分组 选出时间最大的那个货物名称
    结果如下: 员工编号 类型     时间 
      1      a      B货
      1      b      d货
      2      a      f货
      

  5.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([员工编号] int,[类型] varchar(1),[货物名称] varchar(3),[时间] datetime)
    insert [tb]
    select 1,'a','A货','2009-3-1' union all
    select 1,'a','B货','2009-3-10' union all
    select 1,'b','c货','2009-4-1' union all
    select 1,'b','d货','2009-4-10' union all
    select 2,'a','e货','2009-1-15' union all
    select 2,'a','f货','2009-1-1'
     
    ---查询---
    select *
    from tb t
    where not exists(select 1 from tb where 员工编号=t.员工编号 and 类型=t.类型 and 时间>t.时间)---结果---
    员工编号        类型   货物名称 时间                                                     
    ----------- ---- ---- ------------------------------------------------------ 
    1           a    B货   2009-03-10 00:00:00.000
    1           b    d货   2009-04-10 00:00:00.000
    2           a    e货   2009-01-15 00:00:00.000(所影响的行数为 3 行)