select a.*,b.*
from a,b
where a.PRG0200 =b.PRG0200 and
      a.主键=(select top 1 主键 from A b where b.PRG0200 =a.PRG0200 )

解决方案 »

  1.   

    select a.*,b.*
    from a,b
    where a.PRG0200 =b.PRG0200 and
          a.主键=(select top 1 主键 from A b where b.PRG0200 =a.PRG0200 )
      

  2.   

    set rowcount 1
    select a.*,b.*
    from a,b
    where a.PRG0200 =b.PRG0200 and
          a.主键=(select 主键 from A b where b.PRG0200 =a.PRG0200 )
    ---order by ....
      

  3.   

    A 数据31
    32
    32
    32
    33
    33
    33B30
    31
    32
    33我选择的结果是多条
    但A表的ID是不重复的 重复时 选第1条
      

  4.   

    A表得有主键,要不然没办法确定第一条要不这样select idd=identity(int,1,1),* into #t from A order by PRG0200 select a.*,b.*
    from #t a,b
    where a.PRG0200 =b.PRG0200 and
          a.idd=(select top 1 idd from #t b where b.PRG0200 =a.PRG0200 order by b.idd)
      

  5.   

    lsxaa(小李铅笔刀) 我的PRG0200不是自动增号的所以这样select idd=identity(int,1,1),* into #t from A order by PRG0200 
    不行
      

  6.   

    PRG0200   不需要是自动增号的,A表里不是有重复的PRG0200号吗? select idd=identity(int,1,1),* into #t from A order by PRG0200 
    运行这条语句后,给重复的编号加上一个不同的标识,查询的时候用来确定相同PRG0200 号的第一条
    你没有试我写的语句吧 
      

  7.   

    谢谢  lsxaa(小李铅笔刀)能不能用一条Sql语句来完成
      

  8.   

    如果a表有关键字,xluzhong(打麻将一缺三,咋办?) 的sql语句可以用的。
    如果没有的话,试试下面的语句。我在自己机器上实现了
    declare @Series varchar(10)
    declare B_cursor cursor forselect distinct PRG0200 from b
    order by PRG0200
    open PRG0200
    fetch next from B_cursor into @Series
    while @@fetch_status=0
    begin
        select top 1 PRG0200 from a where PRG0200=@Series
        fetch next from B_cursor into @Series
    end
    CLOSE B_cursor
    DEALLOCATE B_cursor建议设关键字,这样速度可以快很多!!