所以我想做下面的SQL代码:
select s.id, s.name,s.city
from stuff s
group by s.name having count(where city and name are identical) > 1
要生成以下内容,(但忽略只有名称或仅匹配城市的位置,它必须位于两列上):
id name city
904834 jim London
904835 jim London
90145 Fred Paris
90132 Fred Paris
90133 Fred Paris
答案 0 :(得分:112)
对id
和name
对<{1}}重复city
。
select s.id, t.*
from [stuff] s
join (
select name, city, count(*) as qty
from [stuff]
group by name, city
having count(*) > 1
) t on s.name = t.name and s.city = t.city
答案 1 :(得分:33)
SELECT name, city, count(*) as qty
FROM stuff
GROUP BY name, city HAVING count(*)> 1
答案 2 :(得分:7)
这样的事情可以解决问题。不知道性能,所以做一些测试。
select
id, name, city
from
[stuff] s
where
1 < (select count(*) from [stuff] i where i.city = s.city and i.name = s.name)
答案 3 :(得分:3)
使用count(*) over(partition by...)
提供了一种简单有效的方法来查找不必要的重复,同时还列出了所有受影响的行和所有需要的列:
SELECT
t.*
FROM (
SELECT
s.*
, COUNT(*) OVER (PARTITION BY s.name, s.city) AS qty
FROM stuff s
) t
WHERE t.qty > 1
ORDER BY t.name, t.city
最新的RDBMS版本支持count(*) over(partition by...)
MySQL V 8.0引入的“窗口函数”,如下所示(在MySQL 8.0中)
CREATE TABLE stuff( id INTEGER NOT NULL ,name VARCHAR(60) NOT NULL ,city VARCHAR(60) NOT NULL );
INSERT INTO stuff(id,name,city) VALUES (904834,'jim','London') , (904835,'jim','London') , (90145,'Fred','Paris') , (90132,'Fred','Paris') , (90133,'Fred','Paris') , (923457,'Barney','New York') # not expected in result ;
SELECT t.* FROM ( SELECT s.* , COUNT(*) OVER (PARTITION BY s.name, s.city) AS qty FROM stuff s ) t WHERE t.qty > 1 ORDER BY t.name, t.city
id | name | city | qty -----: | :--- | :----- | --: 90145 | Fred | Paris | 3 90132 | Fred | Paris | 3 90133 | Fred | Paris | 3 904834 | jim | London | 2 904835 | jim | London | 2
db <>提琴here
窗口函数。 MySQL现在支持窗口函数,对于查询中的每一行,都使用与 那排。这些包括诸如RANK(),LAG()和NTILE()之类的函数。 此外,现在可以将几个现有的聚合函数用作 窗口功能;例如SUM()和AVG()。了解更多信息, 参见Section 12.21, “Window Functions”。
答案 4 :(得分:2)
你必须自己加入东西并匹配名称和城市。然后按计数分组。
select
s.id, s.name, s.city
from stuff s join stuff p ON (
s.name = p.city OR s.city = p.name
)
group by s.name having count(s.name) > 1
答案 5 :(得分:0)
给定一个包含70列的登台表,只有4列表示重复, 此代码将返回有问题的列:
SELECT
COUNT(*)
,LTRIM(RTRIM(S.TransactionDate))
,LTRIM(RTRIM(S.TransactionTime))
,LTRIM(RTRIM(S.TransactionTicketNumber))
,LTRIM(RTRIM(GrossCost))
FROM Staging.dbo.Stage S
GROUP BY
LTRIM(RTRIM(S.TransactionDate))
,LTRIM(RTRIM(S.TransactionTime))
,LTRIM(RTRIM(S.TransactionTicketNumber))
,LTRIM(RTRIM(GrossCost))
HAVING COUNT(*) > 1
答案 6 :(得分:0)
这篇文章对游戏来说有点晚了,但是我发现这种方式非常灵活/高效
select
s1.id
,s1.name
,s1.city
from
stuff s1
,stuff s2
Where
s1.id <> s2.id
and s1.name = s2.name
and s1.city = s2.city
答案 7 :(得分:-1)
SELECT Feild1, Feild2, COUNT(*)
FROM table name
GROUP BY Feild1, Feild2
HAVING COUNT(*)>1
这将为您提供所有答案。