27,579
社区成员
发帖
与我相关
我的任务
分享
select id=row_number() over(order by CustomerName,CompanyName)
,CustomerName,CompanyName
,Address=stuff((select ''+Address from Customer where CustomerName=t.CustomerName and CompanyName=t.CompanyName for xml path('')),1,1,'')
from Customer t
group by CustomerName,CompanyName
create table Customer(ID int,CustomerName varchar(20),CompanyName varchar(20),Address varchar(20))
GO
insert Customer select 1,'A','CC','A1'
insert Customer select 2,'A','CC','A12'
insert Customer select 3,'B','BB','B1'
insert Customer select 4,'B','BB','B1'
insert Customer select 5,'B','DD','B2'
insert Customer select 6,'C','DD','C1'
insert Customer select 7,'D','FF','D1'
insert Customer select 8,'F','DD','F1'
go
SELECT
*
FROM
CUSTOMER T
WHERE
EXISTS(SELECT 1
FROM CUSTOMER
WHERE COMPANYNAME = T.COMPANYNAME AND CUSTOMERNAME=T.CUSTOMERNAME AND ADDRESS!=T.ADDRESS)
drop table Customer
/*
ID CustomerName CompanyName Address
----------- -------------------- -------------------- --------------------
1 A CC A1
2 A CC A12
(所影响的行数为 2 行)
*/
select CustomerName, CompanyName
from Customer
group by CustomerName, CompanyName
having count(1) > 1
declare @customer table (id int ,customername nvarchar(10),companyname nvarchar(10),
address nvarchar(10))
insert into @customer select 1,'a','cc','a1'
union all select 2,'a','cc','a12'
union all select 3,'b','bb','b1'
union all select 4,'b','bb','b1'
union all select 5,'b','dd','b2'
union all select 6,'c','dd','c1'
union all select 7,'d','ff','d1'
union all select 8,'f','dd','f1'
select customername,companyname,address from @customer
group by customername,companyname,address
create table Customer(ID int,CustomerName varchar(20),CompanyName varchar(20),Address varchar(20))
GO
insert Customer select 1,'A','CC','A1'
insert Customer select 2,'A','CC','A12'
insert Customer select 3,'B','BB','B1'
insert Customer select 4,'B','BB','B1'
insert Customer select 5,'B','DD','B2'
insert Customer select 6,'C','DD','C1'
insert Customer select 7,'D','FF','D1'
insert Customer select 8,'F','DD','F1'
--嵌套循环
select * from Customer a
where exists (select 1 from Customer where a.CompanyName=CompanyName and a.CustomerName=CustomerName
and a.Address!=Address)
/*
ID CustomerName CompanyName Address
1 A CC A1
2 A CC
*/
select * from Customer a
where exists (select 1 from Customer where a.CompanyName=CompanyName and a.CustomerName=CustomerName
and a.Address<>Address)
/*
ID CustomerName CompanyName Address
1 A CC A1
2 A CC A12
*/
--合并联接
select a.*
from Customer a
join (
select CustomerName,CompanyName
from Customer
group by CustomerName,CompanyName
having COUNT(distinct Address) > 1
) b on a.CompanyName=b.CompanyName and a.CustomerName=b.CustomerName
/*
ID CustomerName CompanyName Address
1 A CC A1
2 A CC A12
*/
select s.* from tb where Customer s,Customer t where s.CustomerName=t.CustomerName and s.CompanyName=t.CompanyName and s.address<>t.address
declare @Customer table(ID int, CustomerName varchar(5), CompanyName varchar(5), Address varchar(5) )
insert @Customer select 1 , 'A' , 'CC' , 'A1'
insert @Customer select 2 , 'A' , 'CC' , 'A12'
insert @Customer select 3 , 'B' , 'BB' , 'B1'
insert @Customer select 4 , 'B' , 'BB' , 'B1'
insert @Customer select 5 , 'B' , 'DD' , 'B2'
insert @Customer select 6 , 'C' , 'DD' , 'C1'
insert @Customer select 7 , 'D' , 'FF' , 'D1'
insert @Customer select 8 , 'F' , 'DD' , 'F1'
-- 要求找出同一个CustomerName(CustomerName可能会有重复的名称),同一个公司名 不同地址的数据列表
-- ID CustomerName CompanyName Address
-- ---------------------------------------------
select * from @Customer a where not exists(select 1 from @Customer where CustomerName=a.CustomerName
and id<>a.id and (CompanyName<>a.CompanyName or (CompanyName=a.CompanyName and Address=a.Address)))
/*
ID CustomerName CompanyName Address
----------- ------------ ----------- -------
1 A CC A1
2 A CC A12
6 C DD C1
7 D FF D1
8 F DD F1
(所影响的行数为 5 行)
*/
select * from Customer a where not exists(select 1 from Customer where CustomerName=a.CustomerName
and id<>a.id and (CompanyName<>a.CompanyName or (CompanyName=a.CompanyName and Address=a.Address))