我建了一个视图,怎么从符合条件的记录中每个ID只选一条出来?
结构:
id    company_name   name flag7          test1       2   1
8          test2       3   1
9          test3       4   1
9          test3       3   1
10         test4       5   1
11         test5      null 1
符合flag=1条件的记录有5条
其中id=9的有两条记录
但我想在flag=1的查询语句里返回id=9的一条记录及:
id    company_name   name flag7          test1       2   1
8          test2       3   1
9          test3       4   1
10         test4       5   1
11         test5      null 1至于id=9的两条记录里随便拿一条都行
当然能加上条件选择一条更好
该怎么做啊

解决方案 »

  1.   

    select 1 * from urtable where Flag = 1
      

  2.   

    select  top 1   *   from   urtable   where   Flag   =   1
      

  3.   


    select * from tb a where a.flag=1 and not exists(select 1 from tb b where a.Id=b.Id and a.name<b.name)
    --找name最小的。
      

  4.   

    yun
    没看清需求
    select distinct company_name from urtable where flag = 1
      

  5.   

    分明就是用distinct.
    不过为什么连ID都可以重复呢?
      

  6.   

    distinct
    不行
    我要把name列也一起输出的
      

  7.   

    select * from urtable where flag =1
    and company_name exists(select distinct company_name from urtable)
      

  8.   

    SELECT DISTICT id,company_name,[name],flag FROM Tablename WHERE flag=1
      

  9.   

    select distinct id,company_name,max(name) name,flag from tablename group by id,company_name,flag 
     
      

  10.   

    SELECT * from urtable where flag = 1
    and company_name exists(select distinct  company_name   from   urtable )
    and [name] exists(select min([name])   from   urtable group by [company_name])
      

  11.   

    用 distinct
    select distinct * from  视图
      

  12.   


    用   distinct 
    select   distinct   company_name, name,flag   from     视图
      

  13.   

    distinct 是重复只保留1条