[英]How do I see if there are multiple rows with an identical value in particular column?
I'm looking for an efficient way to exclude rows from my SELECT
statement WHERE
more than one row is returned with an identical value for a certain column.我正在寻找一种有效的方法来从我的
SELECT
语句中WHERE
行,其中针对某一列返回了多个具有相同值的行。
Specifically, I am selecting a bunch of accounts, but need to exclude accounts where more than one is found with the same SSN associated.具体来说,我选择了一堆帐户,但需要排除发现多个具有相同 SSN 关联的帐户。
this will return all SSNs with exactly 1 row这将返回恰好有 1 行的所有 SSN
select ssn,count(*)
from SomeTable
group by ssn
having count(*) = 1
this will return all SSNs with more than 1 row这将返回所有超过 1 行的 SSN
select ssn,count(*)
from SomeTable
group by ssn
having count(*) > 1
Your full query would be like this (will work on SQL Server 7 and up)您的完整查询将是这样的(将在 SQL Server 7 及更高版本上工作)
select a.* from account a
join(
select ssn
from SomeTable
group by ssn
having count(*) = 1) s on a.ssn = s.ssn
For SQL 2005 or above you can try this:对于 SQL 2005 或更高版本,您可以尝试以下操作:
WITH qry AS
(
SELECT a.*,
COUNT(*) OVER(PARTITION BY ssn) dup_count
FROM accounts a
)
SELECT *
FROM qry
WHERE dup_count = 1
For SQL 2000 and 7:对于 SQL 2000 和 7:
SELECT a.*
FROM accounts a INNER JOIN
(
SELECT ssn
FROM accounts b
GROUP BY ssn
HAVING COUNT(1) = 1
) b ON a.ssn = b.ssn
SELECT *
FROM #Temp
WHERE SSN NOT IN (SELECT ssn FROM #Temp GROUP BY ssn HAVING COUNT(ssn) > 1)
Thank you all for your detailed suggestions.谢谢大家的详细建议。 When it was all said and done, I needed to use a correlated subquery .
当一切都说完了,我需要使用相关的子查询。 Essentially, this is what I had to do:
本质上,这就是我必须做的:
SELECT acn, ssn, [date] FROM Account a
WHERE NOT EXISTS (SELECT 1 FROM Account WHERE ssn = a.ssn AND [date] < a.[date])
Hope this helps someone.希望这可以帮助某人。
I never updated this... In my final submission, I achieved this through a left join to increase efficiency (the correlated subquery was not acceptable as it took a significant amount of time to run, checking each record against over 150K others).我从来没有更新过这个......在我的最终提交中,我通过左连接来提高效率(相关子查询是不可接受的,因为它需要大量时间来运行,检查每条记录与超过 150K 其他记录)。
Here is what had to be done to solve my problem:这是解决我的问题必须做的事情:
SELECT acn, ssn
FROM Account a
LEFT JOIN (SELECT ssn, COUNT(1) AS counter FROM Account
GROUP BY ssn) AS counters
ON a.ssn = counters.ssn
WHERE counter IS NULL OR counter = 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.