tabelA:id    name        postdate
1     aaa          2006-6-1
2     bbb          2006-6-33     aaa          2006-6-14     bbb          2006-6-3
用一条SQL语句查出1     aaa          2006-6-1
2     bbb          2006-6-3
也就是说将同时满足 name  postdate 的重复记录去掉?谢谢!

解决方案 »

  1.   

    select min(id) id , name ,postdate from tb group by name,postdate
      

  2.   

    declare @a table (id int,name varchar(10),postdate varchar(8))
    insert into @a select 1,'aaa','2006-6-1'
    union all
    select 2,'bbb','2006-6-3'
    union all
    select 3,'aaa','2006-6-1'
    union all
    select 4,'bbb','2006-6-3'
    select name,postdate from @a group by name,postdate
      

  3.   

    如果id是自增的,也可以这样写:
    select top 2 * from  tableA order by id asc
      

  4.   

    select min(id) id , name ,postdate from tb group by name,postdate
      

  5.   

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

  6.   


    create table tablea (
    id int,
    name    varchar(10),
    postdate varchar(10)
    )insert into tablea 
    select 1,'aaa','2006-6-1' union all
    select 2,'bbb','2006-6-3' union all
    select 3,'aaa','2006-6-1' union all
    select 4,'bbb','2006-6-3' 
    select * from tablea
    /*id          name       postdate   
    ----------- ---------- ---------- 
    1           aaa        2006-6-1
    2           bbb        2006-6-3
    3           aaa        2006-6-1
    4           bbb        2006-6-3(所影响的行数为 4 行)
    */select min(id) as  id , name ,postdate 
    from tablea 
    group by name,postdate
    /*
    id          name       postdate   
    ----------- ---------- ---------- 
    1           aaa        2006-6-1
    2           bbb        2006-6-3(所影响的行数为 2 行)
    */
      

  7.   

    --如果要求id是连续且递增的,可以这样
    select identity(int,1,1) AS id,name,postdate into #
    from tablea 
    group by name,postdateselect * 
    from #
    /*
    id          name       postdate   
    ----------- ---------- ---------- 
    1           aaa        2006-6-1
    2           bbb        2006-6-3(所影响的行数为 2 行)
    */