select distinct dingdan From orders where niming=0想要排重复的是dingdan字段,以上语句可以排除重复,但得不到其它字段数据
select distinct(dingdan),actionid From orders where niming=0
这样子能得到其它字段数据,但无法排除重复的dingdan字段,请指点。

解决方案 »

  1.   

    declare @t table (ID int,  Name varchar(10))
    insert into @t select 1,'zhang'
    insert into @t select 2,'zhang'
    insert into @t select 3,'li'
    insert into @t select 4,'li'
    insert into @t select 5,'zhou'
    insert into @t select 6,'zhou'
    insert into @t select 7,'zhou'
    insert into @t select 8,'ding'select * from @t a where not exists(select 1 from @t b where b.id<a.id and b.name=a.name)
    --方法一
    select * from @t where id in(select min(id) from @t group by name)  
    --方法二
    select * from @t where id not in(select max(id) from @t group by name)  
    --方法三--结果
    /*
    ID Name
    1       zhang
    3       li
    5       zhou
    8       ding
    */
      

  2.   

    select *
    From orders a
    where niming=0
    and exists(select top 1 * from  orders where dingdan=a.dingdan)
      

  3.   

    select top1 *
    From orders a
    where niming=0
    and exists(select 1 from  orders where dingdan=a.dingdan)
      

  4.   

    select identity(int,1,1) as autoID, * into #Tmp from orders select min(autoID) as autoID 
    into #Tmp2 
    from #Tmp 
    group by dingdanselect * from #Tmp where autoID in(select autoID from #tmp2)