我有一个名为员工的表,此表中有许多记录。以下是一些示例数据:
fullname | address | city
-----------------------------
AA address1 City1
AA address3 City1
AA address8 City2
BB address5 City2
BB address2 City1
CC address6 City1
CC address7 City2
DD address4 City1
我希望在sql server中有一个SELECT
查询,它只显示基于列fullname
和city
的重复记录。对于给定的数据并考虑条件,只有前两个记录是重复的。所以我的预期输出应该如下:
fullname | address | city
-----------------------------
AA address1 City1
AA address3 City1
要获得此输出,我已编写此查询:
select fullname, city from employee group by fullname, city having count(*)>1
如您所见,它仅选择两列,因此它提供以下输出:
fullname | city
------------------
AA City1
如果我重新编写如下查询:
select fullname, city, address from employee group by fullname, city, address
having count(*)>1
不幸的是它显示没有记录!有人可以帮我写出正确的查询以获得预期的结果吗?
答案 0 :(得分:5)
您可以将其用作窗口聚合来访问其他列,而不是分组COUNT
SELECT fullname,
address,
city
FROM (SELECT *,
COUNT(*) OVER (PARTITION BY fullname, city) AS cnt
FROM employee) e
WHERE cnt > 1
答案 1 :(得分:1)
同意上述答案。 但是如果您不想使用可能无法在所有数据库上正常工作的Windows功能,您可以在城市和全名后加入自己的组,然后获取地址
Select employee.* from employee
join (select fullname, city from employee group by fullname, city having count(*)>1) q1
on q1.fullname = employee.fullname and q1.city = employee.city
答案 2 :(得分:1)
尝试以下代码:
create table ##Employee
(Fullname varchar(25),
Address varchar(25),
City varchar(25))
insert into ##Employee values
( 'AA', 'address1', 'City1')
,( 'AA', 'address3', 'City1')
,( 'AA', 'address8', 'City2')
,( 'BB', 'address5', 'City2')
,( 'BB', 'address2', 'City1')
,( 'CC', 'address6', 'City1')
,( 'CC', 'address7', 'City2')
select E.* from ##Employee E
cross apply(
select Fullname,City,count(Fullname) cnt from ##Employee
group by Fullname,City
having Count(Fullname)>1)x
where E.Fullname=x.Fullname
and E.City=x.City
答案 3 :(得分:1)
如果您有唯一的ID或地址总是不同,您可以尝试:
select e.*
from employee e
where exists (select 1
from employee e2
where e2.fullname = e.fullname and e2.city = e.city and
e2.address <> e.address -- or id or some other unique column
);
虽然我可能会使用窗口函数方法,但您可能会发现在某些情况下,这会更快(特别是如果您在employee(fullname, city, address)
上有索引)。
答案 4 :(得分:0)
您可以使用解决方案:
DECLARE @Employee TABLE
(
Fullname VARCHAR(25),
[Address] VARCHAR(25),
City VARCHAR(25)
)
INSERT INTO @Employee VALUES
('AA', 'address1', 'City1')
,('AA', 'address1', 'City1')
,('AA', 'address3', 'City1')
,('AA', 'address8', 'City2')
,('BB', 'address5', 'City2')
,('BB', 'address2', 'City1')
,('CC', 'address6', 'City1')
,('CC', 'address7', 'City2')
;WITH cte AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY FullName, [Address], [City] ORDER BY Fullname) AS sl,
HashBytes('MD5', FullName + [Address] + [City]) AS RecordId
FROM @Employee AS e
)
SELECT c.FullName,
c.[Address],
c.City
FROM cte AS c
INNER JOIN cte AS c1
ON c.RecordId = c1.RecordId
WHERE c.sl = 2
结果:
FullName Address City
AA address1 City1
AA address1 City1
答案 5 :(得分:-1)
SELECT Feild1, Feild2, COUNT() FROM table name GROUP BY Feild1, Feild2 HAVING COUNT()>1
这会给你所有的答案