select distinct a,b 这种不能处理的,是要列出两个字段里不都不重复的字段不想用临时表处理。字段a(外型设计),字段b(结构设计),字段c(产品名称)
技术员有可能是这个产品的外型设计,也有可能是那个产品的结构设计,也有可能是结构、外型设计要求列出现有技术员手上,都负责那几个产品distinct 好象只能接受单个过滤,两个字段好象不行

解决方案 »

  1.   

    select * from tb t where not exists(select 1 from tb where a=t.a and b<t.b)
      

  2.   

    select * from tb t where not exists(select 1 from tb where col1=t.col1
    and col2=t.col2 and 主鍵列>t.主鍵列(或者id>t.id))
      

  3.   

    select a , b , max(c) c from tb group by a , b
    select a , b , min(c) c from tb group by a , b
      

  4.   


    if object_id('tb')is not null drop table tb
    go
    create table tb(a int,b int,name varchar(10))
    insert tb select
    1, 2,  '普检'  union all select
    2, 2,  'a'  union all select
    1, 3,  'b'  union all select
    2, 1,  'c'  union all select
    4, 2,  'd'  union all select
    1, 5,  'e' 
    select distinct name, a from 
    (select a,name from tb
    union all 
    select b,name from tb)t
    where a=2name       a
    ---------- -----------
    a          2
    c          2
    d          2
    普检         2(4 行受影响)???
      

  5.   

    ID   产品  外型设计 结构设计
    1001 A    张三      张三
    1002 B    张一      李一
    1003 C    张三      李一
    1004 D    李一      李二要的结果是  李一  B、C、D  共负责三个产品
              李二   D       共负责一个产品 
              张一   B       共负责一个产品 
              张三   A、C     共负责二个产品
      

  6.   

    select name , count(1) 
    from 
    (
      select 产品 , 外型设计 name from tb
      union 
      select 产品 , 结构设计 name from tb
    ) t
    group by name
      

  7.   


    if object_id('tb')is not null drop table tb
    go
    create table tb(ID int, 产品 varchar(2), 外型设计 varchar(20),结构设计 varchar(20))
    insert tb select
    1001, 'A'  ,  '张三',      '张三' union all select
    1002, 'B' ,   '张一' ,     '李一' union all select
    1003, 'C' ,   '张三'  ,    '李一' union all select
    1004, 'D' ,   '李一'   ,   '李二' if object_id('f_str')is not null drop function f_str
    go
    create function f_str(@s varchar(20))
    returns varchar(400)
    as
    begin 
    declare @str varchar(400)
    select @str=isnull(@str+',','')+产品
    from tb where 外型设计=@s or 结构设计=@s return @str
    end
    go select s ,cp=dbo.f_str(s) from 
    (select  外型设计 as s from tb
    union 
    select  结构设计 from tb)t
    s                    cp
    -------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    李二                   D
    李一                   B,C,D
    张三                   A,C
    张一                   B(4 行受影响)
      

  8.   

    ID  产品  外型设计                               结构设计 
    1001 A    张三 张四(多个人名,之间用空格区分)     张三 (多个人名,之间用空格区分)
    1002 B    张一                                      李一 
    1003 C    张三 李二                                 李一 
    1004 D    李一                                      李二 李一 李三 张五如果再复杂一些呢?
      

  9.   

    按照dawugui的类推,多少都是一样,
    就等于是把很多自动合并成一个,然后再distinct,就这么回事。
      

  10.   


    if object_id('tb')is not null drop table tb
    go
    create table tb(ID int, 产品 varchar(2), 外型设计 varchar(20),结构设计 varchar(20))
    insert tb select
    1001, 'A'  ,  '张三 张四',      '张三' union all select
    1002, 'B' ,   '张一' ,          '李一' union all select
    1003, 'C' ,   '张三 李二'  ,    '李一' union all select
    1004, 'D' ,   '李一'   ,        '李二 李一 李三 张五' 
    if object_id('f_str')is not null drop function f_str
    go
    create function f_str(@s varchar(20))
    returns varchar(400)
    as
    begin 
        declare @str varchar(400)
        select @str=isnull(@str+',','')+产品
        from tc where s=@s      return @str
    end
    go 
    select top 1000 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns bif object_id('tc')is not null drop table tc     --创建辅助表
    go
    create table tc(产品 varchar(20),s varchar(20))insert tc Select 
    a.产品,s=substring(a.s,b.ID,charindex(',',a.s+',',b.ID)-b.ID)
    from 
        (select 产品,replace(外型设计,' ',',') as s from tb union all select 产品,replace(结构设计,' ',',') from tb)a
    ,#Num b
    where
        charindex(',',','+a.s,b.ID)=b.ID 
    ---结果
    -----------------------------------------
    select  s ,cp=dbo.f_str(s) from tc
    group by ss                    cp
    -------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    李二                   D,C
    李三                   D
    李一                   D,B,C,D
    张三                   A,C,A
    张四                   A
    张五                   D
    张一                   B
    drop table #num
      

  11.   


    ---修改一下if object_id('tb')is not null drop table tb
    go
    create table tb(ID int, 产品 varchar(2), 外型设计 varchar(20),结构设计 varchar(20))
    insert tb select
    1001, 'A'  ,  '张三 张四',      '张三' union all select
    1002, 'B' ,   '张一' ,          '李一' union all select
    1003, 'C' ,   '张三 李二'  ,    '李一' union all select
    1004, 'D' ,   '李一'   ,        '李二 李一 李三 张五' 
    if object_id('f_str')is not null drop function f_str
    go
    create function f_str(@s varchar(20))
    returns varchar(400)
    as
    begin 
        declare @str varchar(400)
        select @str=isnull(@str+',','')+产品
        from tc where s=@s      return @str
    end
    go 
    select top 1000 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns bif object_id('tc')is not null drop table tc     --创建辅助表
    go
    create table tc(产品 varchar(20),s varchar(20))insert tc Select distinct  
    a.产品,s=substring(a.s,b.ID,charindex(',',a.s+',',b.ID)-b.ID)
    from 
        (select 产品,replace(外型设计,' ',',') as s from tb union all select 产品,replace(结构设计,' ',',') from tb)a
    ,#Num b
    where
        charindex(',',','+a.s,b.ID)=b.ID 
    select * from tc
    ---结果
    -----------------------------------------
    select s ,cp=dbo.f_str(s) from tc
    group by ss                    cp
    -------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    李二                   C,D
    李三                   D
    李一                   B,C,D
    张三                   A,C
    张四                   A
    张五                   D
    张一                   B(7 行受影响)
    drop table #num