有表结构及数据如下:
CustomerID Value
1 a
1 b
1 c
1 d
2 b
2 c
2 d
2 e
3 c
3 d
3 e
3 f
求一SQL语句,查询出含有多个value值的CustomerID
如: 查询含有a,b的,则CustomerID为1
查询含有b,c的,则CustomerID为1,2
查询含有c,d的,则CustomerID为1,2,3

解决方案 »

  1.   

    参考:create table tbltest(列A int,列C varchar(100))
    go
    insert into tbltest
    select 1,'A' union all
    select 1,'B' union all
    select 1,'C' union all
    select 1,'F' union all
    select 1,'G' union all
    select 2,'E' union all
    select 2,'F' union all
    select 2,'F'
    go--写一个聚合函数:
    create function dbo.fn_Merge(@F1 int)
    returns varchar(8000)
    as
    begin
       declare @r varchar(8000)
       set @r=''
       select @r=@r+','+列C from tbltest where 列A=@F1 -- 先加逗号,再串起来
       return stuff(@r,1,1,'') -- 删除最前面加的逗号
    end
    go-- 调用函数
    select 列A, dbo.fn_Merge(列A) as 列C 
    from tbltest 
    group by 列A
    go--删除测试数据
    drop table tbltest
    drop function fn_Merge--查看结果
    /*
    列A  列C
    1    A,B,C,F,G
    2    E,F,F
    */
      

  2.   

    select CustomerID from table1 ,(select CustomerID,count(*) as num from table1 group by CustomerID) as t where table1.CustomerID = t.CustomerID and t.num > 1
      

  3.   

    create table tbltest(CustomerID  varchar(100),Value varchar(100))
    go
    insert into tbltest
    select 1,'a' union all
    select 1,'b' union all
    select 1,'c' union all
    select 1,'d' union all
    select 2,'b' union all
    select 2,'c' union all
    select 2,'d' union all
    select 2,'e' union all
    select 3,'c' union all
    select 3,'d' union all
    select 3,'e' union all
    select 3,'f'
    go--写一个聚合函数:
    create function dbo.fn_Merge(@F1 varchar(100))
    returns varchar(8000)
    as
    begin
       declare @r varchar(8000)
       set @r=''
       select @r=@r+','+CustomerID from tbltest where Value=@F1
       return stuff(@r,1,1,'')
    end
    go-- 调用函数
    select Value, dbo.fn_Merge(Value) as CustomerID 
    from tbltest 
    group by Value
    go--删除测试数据
    drop table tbltest
    drop function fn_Merge--查看结果
    /*
    Value  CustomerID
    a 1
    b 1,2
    c 1,2,3
    d 1,2,3
    e 2,3
    f 3
    */
      

  4.   

    select distinct table1.CustomerID from table1 ,(select CustomerID,count(*) as num from table1 group by CustomerID) as t where table1.CustomerID = t.CustomerID and t.num > 1
      

  5.   

    gc_ding(施主,给个妞泡好么) :还是不太明白
      

  6.   

    select distinct CustomerID from table where [Value]in('b','c')
    --用distinct 去掉重复值
      

  7.   

    create function fn_getstr(@value varchar(100))
    returns varchar(4000)
    as
    begin
       declare @str varchar(8000)
       set @str=''
       select @str=@str+','+CustomerID from table where Value=@value
       set @str=substring(@str,2,len(@str))
       return @str
    end
    go
    select value,dbo.fn_getstr(value) as CustomerID 
    from table
    go
      

  8.   

    gc_ding(施主,给个妞泡好么) :我是想要做个这样的查询,以value的取值做为条件,查询符合条件的CustomerID。条件是变化的,最终是要查询出CustomerID,在这个CustomerID相关的记录中,必须要包含条件中所有的Value的取值。
      

  9.   

    以value的取值做为条件,会出现这样的情况么:
    a,b
    a,b,c
    a
    a,b,c,d
    a,b,d,d,e,f
      

  10.   

    求一SQL语句,查询出含有多个value值的CustomerID与求一SQL语句,查询出含有多个CustomerID值的Value两种问法,大家好像没弄清吧,第一种问法 我测了下一步 
    gc_ding(施主,给个妞泡好么) 做对了第二种问法,第二楼的人做对了
      

  11.   

    gc_ding(施主,给个妞泡好么) :可能的
      

  12.   

    gc_ding(施主,给个妞泡好么) :实际情况中的组合可能会更多,而且实际的数据表中的数据是海量的。
      

  13.   

    是不是得先求出value的各种排列组合?
      

  14.   

    mengmou()mengmou() :不用的,value的值是参数来的
      

  15.   

    用存储过程传参就可以实现了,楼主要怎样的效果,显示的CustomerID是横向还是竖向
      

  16.   

    SELECT CustomerID,COUNT(*) AS N 
          FROM 表
         WHERE COUNT(*)>1
      GROUP BY CustomerID
      

  17.   

    create table T(CustomerID int, Value char(1))
    insert T select 1,'a'
    union all select 1,'b'
    union all select 1,'c'
    union all select 1,'d'union all select 2,'b'
    union all select 2,'c'
    union all select 2,'d'
    union all select 2,'e'union all select 3,'c'
    union all select 3,'d'
    union all select 3,'e'
    union all select 3, 'f'create function fun(@str varchar(100))
    returns varchar(100)
    as
    begin
    declare @re varchar(100)
    set @re='' declare @len int
    set @len=len(@str)-len(replace(@str, ',', ''))+1 select @re=@re+','+rtrim(CustomerID) from T where charindex(Value, @str)>0 group by CustomerID having count(*)=@len

    return(stuff(@re, 1, 1, ''))
    end
    select dbo.fun('a,b')
    --result
    ---------------------------------------------------------------------------------------------------- 
    1(1 row(s) affected)
    select dbo.fun('b,c')
    --result
    ---------------------------------------------------------------------------------------------------- 
    1,2(1 row(s) affected)select dbo.fun('c,d')
    --result
    ---------------------------------------------------------------------------------------------------- 
    1,2,3(1 row(s) affected)
      

  18.   

    http://hi.baidu.com/boshiclub
    我们一起创业,我们一起发财,我们一起快乐,我们一起努力!
      

  19.   

    roy_88(早晨一缕阳光!!) :要纵向的,即列
      

  20.   

    marco08(天道酬勤) :这种方法有个问题,因为实际上我的value值不是定长的,还有就是能不能查询出的CustomerID是按列显示的,因为实际中可能一组value 值查询出的对应的CustomerID有几十万个的 
      

  21.   

    declare @tab table (CustomerID  varchar(100),Value varchar(100))insert into @tab
    select 1,'a' union all
    select 1,'b' union all
    select 1,'c' union all
    select 1,'d' union all
    select 2,'b' union all
    select 2,'c' union all
    select 2,'d' union all
    select 2,'e' union all
    select 3,'c' union all
    select 3,'d' union all
    select 3,'e' union all
    select 3,'f'Select customerid  From (Select  customerid From @tab Where value = 'a' or value='b')c
           group by customerid 
           having count(customerid)>1