select 客户代号,客户名称
from 表名
group by 客户代号,客户名称,营业机构
having count(*)>1
解决方案 »
- 关于VB操作SQL2005数据库的一个问题 本人比较菜 问的特简单 望指导
- 如何组织成一个新表(或视图)
- 将一个临时表#A中的某一个字段的值向另一个临时表#B的第一个字段中插入值,语句怎么写?
- 存储过程中的set identity_insert <table> on的作用?
- sql菜比求帮助、
- SQL Server的调试
- 用sql 2008 导入导出向导(64位)连接oracle数据库
- 如何获得SQLSERVER所有数据库的空间实际使用、未使用以及总的大小
- 请教高手:关于一个连接查询的问题,详情见内,在线等待
- 如何提高C/S系统的速度(急)
- 怎么编一个通过身份证识别男女的存储过程?
- SQL server 2000与SQL server 2005那个更好上点
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
where exists(select 1 from yourtable where 客户代号=t.客户代号 and 营业机构=t.营业机构 and 客户购买的商品='APPLE') and exists(select 1 from yourtable where 客户代号=t.客户代号 and 营业机构=t.营业机构 and 客户购买的商品='BANANA')
from 表名
where 客户购买的商品 in('APPLE','BANANA')
group by 客户代号,客户名称
having count(*)>1
from 表名
where 客户购买的商品 in ('APPLE','BANANA')
group by 客户代号,客户名称,营业机构
having count(*)>1
(
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
*/
from 表名
group by 客户代号,客户名称,营业机构
having count(*)>1
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
难道我的表达能力这么差吗,你的句子不对.我要的是同一客户在同一机构买了同时买了香蕉和苹果了客户清单,但这个客户可能在同一机构没有买香蕉和苹果,但买了其他很多种水果,那您写的就把这种情况也统计进去了,谢谢
from 表名
where 客户购买的商品 in ('APPLE','BANANA')
group by 客户代号,客户名称,营业机构
having count(*)>1
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
你那句错的。如果有重复记录,比如2条记录都是
1, 'A', '001', 'APPLE'
而没有BANANA的话
按照你的句子也会选出来,这是错的。
select distinct 客户代号,客户名称 from 表名 t
where 客户购买的商品='APPLE'
and exists(select * from 表名 where 客户代号=t.客户代号 and 客户购买的商品='BANANA')
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.客户代号
(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.客户代号
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')
的可以,待我检查一下结果完整否
别上个网还把自己弄郁闷了
select distinct 客户代号,客户名称 from 表名 t
where 客户购买的商品='APPLE'
and exists(select * from 表名 where 客户代号=t.客户代号
and 营业机构=t.营业机构 and 客户购买的商品='BANANA')不要生气,可以结贴了