select 客户代号,客户名称
from 表名
group by 客户代号,客户名称,营业机构
having count(*)>1

解决方案 »

  1.   

    create table #t(客户代号 varchar(100),客户名称 varchar(100),营业机构 varchar(100),客户购买的商品 varchar(100))insert into #t(客户代号,客户名称,营业机构,客户购买的商品)
    select '1','A','001','APPLE' union
    select '2','B','001','APPLE' union
    select '1','A','003','BANANA' union
    select '4','D','007','BANANA' union
    select '1','A','001','BANANA'select 客户代号,客户名称
    from #t
    group by 客户代号,客户名称,营业机构
    having count(*)>1drop table #t
      

  2.   

    select distinct 客户代号,客户名称 from yourtable t
    where exists(select 1 from yourtable  where 客户代号=t.客户代号 and 营业机构=t.营业机构 and 客户购买的商品='APPLE') and exists(select 1 from yourtable  where 客户代号=t.客户代号 and 营业机构=t.营业机构 and 客户购买的商品='BANANA')
      

  3.   

    select 客户代号,客户名称
    from 表名
    where 客户购买的商品 in('APPLE','BANANA')
    group by 客户代号,客户名称
    having count(*)>1
      

  4.   

    select 客户代号,客户名称
    from 表名
    where 客户购买的商品 in ('APPLE','BANANA')
    group by 客户代号,客户名称,营业机构
    having count(*)>1
      

  5.   

    create table pre
    (
      id int ,
      name varchar(10) , 
      dep   varchar(10) ,
      thing varchar(10)
    )
    insert into pre
    select 1          ,'A'         ,'001'      ,'APPLE'
    union select 2           ,'B'         ,'001'       ,'APPLE'
    union select 1           ,'A'          ,'003'      ,'BANANA'
    union select 4           ,'D'          ,'007'      ,'BANANA'
    union select 1           ,'A'          ,'001'      ,'BANANA'select  a.id as 客户代号 , a.name as 客户名称
    from pre a, pre b
    where a.thing = 'APPLE' and b.thing = 'BANANA'
      and a.id = b.id and a.name = b.name and a.dep = b.dep/*
    客户代号  客户名称
    -------------------------
      1     A
    */
      

  6.   

    --任意多种水果,同一个客户在同一个营业机构,消费多次,即多条记录select 客户代号,客户名称
    from 表名
    group by 客户代号,客户名称,营业机构
    having count(*)>1
      

  7.   

    declare @tb table(khdh int,khmc varchar(10),yyjj varchar(10),khgmdsp varchar(20))
    insert into @tb
    select 1, 'A', '001', 'APPLE'union all
    select 2, 'B', '001', 'APPLE'union all
    select 1, 'A', '003', 'BANANA'union all
    select 4, 'D', '007', 'BANANA'union all
    select 1, 'A', '001', 'BANANA'--测试语句
    select t.khdh as 客户代号,t.khmc as 客户名称 from (
    select khdh,khmc,yyjj,khgmdsp from @tb where khgmdsp='apple'or khgmdsp='banana' group by khdh,khmc,yyjj,khgmdsp
    )t
    group by t.khdh,t.khmc having count(*)>1
      

  8.   

    wangtiecheng(cappuccino),
    难道我的表达能力这么差吗,你的句子不对.我要的是同一客户在同一机构买了同时买了香蕉和苹果了客户清单,但这个客户可能在同一机构没有买香蕉和苹果,但买了其他很多种水果,那您写的就把这种情况也统计进去了,谢谢
      

  9.   

    select 客户代号,客户名称
    from 表名
    where 客户购买的商品 in ('APPLE','BANANA')
    group by 客户代号,客户名称,营业机构
    having count(*)>1
      

  10.   


    create table #t(客户代号 varchar(100),客户名称 varchar(100),营业机构 varchar(100),客户购买的商品 varchar(100))insert into #t(客户代号,客户名称,营业机构,客户购买的商品)
    select '1','A','001','APPLE' union all
    select '1','A','001','APPLE' union all
    select '2','B','001','APPLE' union all
    select '2','B','001','APPLE' union all
    select '2','B','001','APPLE' union all
    select '2','B','001','APPLE' union all
    select '1','A','003','BANANA' union all
    select '4','D','007','BANANA' union all
    select '4','D','007','ttt' union all
    select '4','D','007','sss' union all
    select '1','A','001','BANANA'
    select distinct 客户代号,客户名称 from #t t
    where exists
    (select * from #t  where 客户代号=t.客户代号 and 营业机构=t.营业机构 and 客户购买的商品='APPLE') 
    and exists
    (select * from #t  where 客户代号=t.客户代号 and 营业机构=t.营业机构 and 客户购买的商品='BANANA')drop table #t
      

  11.   

    To wangtiecheng(cappuccino) 
    你那句错的。如果有重复记录,比如2条记录都是
    1, 'A', '001', 'APPLE'
    而没有BANANA的话
    按照你的句子也会选出来,这是错的。
      

  12.   

    不是理解错了就是搞复杂了:
    select distinct 客户代号,客户名称 from 表名 t
    where 客户购买的商品='APPLE'
    and exists(select * from 表名 where 客户代号=t.客户代号 and 客户购买的商品='BANANA')
      

  13.   

    create table #t(客户代号 varchar(100),客户名称 varchar(100),营业机构 varchar(100),客户购买的商品 varchar(100))insert into #t(客户代号,客户名称,营业机构,客户购买的商品)
    select '1','A','001','APPLE' union
    select '2','B','001','APPLE' union
    select '1','A','003','BANANA' union
    select '4','D','007','BANANA' union
    select '1','A','001','BANANA'select a.客户代号,客户名称=max(a.客户名称) from 
    #t a join #t b
    on a.客户代号=b.客户代号 and a.营业机构=b.营业机构 and a.客户购买的商品<>b.客户购买的商品
    group by a.客户代号
      

  14.   

    select a.客户代号,客户名称=max(a.客户名称) from 
    (select * from #t where 客户购买的商品='apple') a join 
    (select * from #t where 客户购买的商品='banana') b 
    on a.客户代号=b.客户代号 and a.营业机构=b.营业机构 and a.客户购买的商品<>b.客户购买的商品
    group by a.客户代号
      

  15.   

    如果都返回为0,哪只能说明你的“客户购买的商品”字段设的是固长字符型如char
      

  16.   

    select distinct 客户代号,客户名称 from yourtable t
    where exists(select 1 from yourtable  where 客户代号=t.客户代号 and 营业机构=t.营业机构 and ltrim(rtrim(客户购买的商品))='APPLE') and exists(select 1 from yourtable  where 客户代号=t.客户代号 and 营业机构=t.营业机构 and ltrim(rtrim(客户购买的商品))='BANANA')
      

  17.   

    pbsql(风云) ( ) 
    的可以,待我检查一下结果完整否
      

  18.   

    I服了YOU,三楼的答案有问题吗?
      

  19.   

    mislrb(上班看看早报,上上CSDN,下班看看电影) ( 有什么好生气的,不懂你,
    别上个网还把自己弄郁闷了
      

  20.   

    我的少了的条件哈,三楼虽然正确,但建议简化,只用一个exists就足够了
      

  21.   

    标准的SQL:
    select distinct 客户代号,客户名称 from 表名 t
    where 客户购买的商品='APPLE'
    and exists(select * from 表名 where 客户代号=t.客户代号
               and 营业机构=t.营业机构 and 客户购买的商品='BANANA')不要生气,可以结贴了