有数据如下:
GUID     BZ       
1 A
2 A
3 C
4 D 我希望查询出的结果为GUID     BZ       
1 A
3 C
4 D
就是希望查出的结果显示的是该表BZ字段重复的最小的GUID,不显示重复的,请问怎么写 谢谢我觉得应该类似下面语句 但是第二句不对
select * into #Tmp from FSDL1
select min(GUID) as GUID into #Tmp2 from #Tmp group by BZ,GUID
select * from #Tmp where GUID in(select GUID from #tmp2)

解决方案 »

  1.   

    SELECT GUID,BZ 
    FROM tabl T where GUID in (select MIN(GUID) from tabl where BZ= T.BZ)
      

  2.   

    --> 测试数据:tab
    if object_id('tab') is not null
    drop table tab---->建表
    create table tab([GUID] int,[BZ] varchar(1))
    insert tab
    select 1,'A' union all
    select 2,'A' union all
    select 3,'C' union all
    select 4,'D'--> 查询结果
    SELECT * FROM tab
    --> 删除表格
    --DROP TABLE tab
    drop table #Tmp2
    drop table #Tmpselect * into #Tmp from tabselect min(a.GUID) as GUID into #Tmp2 
    from #Tmp a,tab b
    where a.BZ=b.BZ
    group by a.BZselect * from #Tmp where GUID in(select GUID from #Tmp2)
      

  3.   

    谢谢wxf163兄台~ 原来我是忘了自链接了 呵呵  这两种我测了下 应该是第一种快些吧 ~