简体   繁体   English

如何查看特定列中是否有多行具有相同的值?

[英]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.

相关问题 如何将共享一个相同值的多行折叠成一行 PRESTO SQL - How do I collapse multiple rows that shares one identical value into a single row PRESTO SQL 如何在onUpgrade中添加列并将现有行设置为特定值? - How do I add a column in onUpgrade and set existing rows to a particular value? 我想 select 行在列中具有特定值但列可以包含多个值 - I want to select rows which has a particular value in the column but the column can contain multiple values 在SQL中,如何获取具有特定列的最大值的行? - In SQL, how do I get the row with the maximum value for a particular column? PL / SQL:如何从3个表中选择行并通过使用特定的列值重新排列行? - PL/SQL:How I can select the rows from the 3 tables and rearrange the rows by using particular column value? 我如何 select 多行,其中一行在列中具有特定值? - How do I select multiple rows where one row has a specific value in a column? SQL-聚合结果集中具有相同行的数据,并根据一列的值消除多行 - SQL - Aggregating data in a result set with identical rows and eliminating multiple rows based on one column's value 如何获得联接中相同行的总和 - How do I get sum of identical rows in a join 如何使用SQL / mySQL选择2列具有相同值的多个且1列具有不同值的行? - How do I use SQL/mySQL to select rows where 2 columns have multiple of the same value and 1 column has a distinct value? 如何编写查询以返回特定列的具有不同值的指定行数? - How do I write a query to return specified number of rows with distinct values for a particular column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM