简体   繁体   English

SQL选择行,其中列值是唯一的(仅出现一次)

[英]SQL select rows, where column value is unique (only appears once)

Given the table 给定表

| id | Name |
| 01 | Bob  |
| 02 | Chad |
| 03 | Bob  |
| 04 | Tim  |
| 05 | Bob  |

I want to select the name and ID, from rows where the name is unique (only appears once) 我想从名称唯一的行中选择名称和ID(仅出现一次)

This is essentially the same as How to select unique values of a column from table? 这与如何从表中选择列的唯一值基本相同 , but notice that the author doesn't need the id, so that problem can be solved by a GROUP BY name HAVING COUNT(name) = 1 ,但请注意作者不需要ID,因此可以通过GROUP BY name HAVING COUNT(name) = 1来解决问题

However, I need to extract the entire row (could be tens or hundreds of columns) including the id, where COUNT(name) = 1 , but I cannot GROUP BY id, name as every combination of those are unique. 但是,我需要提取包括id在内的整个行(可以是数十列或几百列),其中COUNT(name) = 1 ,但是我不能GROUP BY id, name每个组合都是唯一的。

EDIT: 编辑:

Am using Google BigQuery. 我正在使用Google BigQuery。

Expected results: 预期成绩:

| id | Name |
| 02 | Chad |
| 04 | Tim  |

Use correlated subquery 使用相关子查询

DEMO DEMO

select * from tablename a
where not exists (select 1 from tablename b where a.name=b.name having count(*)>1)

OUTPUT: OUTPUT:

id  name
2   Chad
4   Tim

You can use NOT EXISTS : 您可以使用NOT EXISTS

SELECT t.*
FROM table t
WHERE NOT EXISTS (SELECT 1 FROM table t1 WHERE t1.name = t.Name AND t1.id <> t.id);

This would need index on table(id, name) to produce faster result set. 这将需要在table(id, name)上建立索引以产生更快的结果集。

Simply do a GROUP BY . 只需执行GROUP BY Use HAVING to make sure a name is only there once. 使用HAVING确保名称仅存在一次。 Use MIN() to pick the only id for the name. 使用MIN()来选择名称的唯一ID。

select min(id), name
from tablename
group by name
having count(*) = 1

Reading the table only once will increase performance! 只读取一次表将提高性能! (And don't forget to create an index on (name, id).) (并且不要忘记在(名称,id)上创建索引。)

use exists and check uqique name 使用存在并检查uqique名称

   select id,name 
    from table t1 
   where exists ( select 1 from table t2 where t1.name=t2.name

    having count(*)=1
)

Please try this. 请尝试这个。

SELECT 
   DISTINCT id,NAME
FROM
   tableName

You can use multiple subqueries to extract what you need. 您可以使用多个子查询来提取所需的内容。

SELECT * FROM tableName
WHERE name IN (SELECT name FROM (SELECT name, COUNT(name) FROM tableName
                                 GROUP BY name 
                                 HAVING COUNT(name) = 1) AS subQuery)

How about a simple aggregation? 简单的聚合怎么样?

select any_value(id), name
from t
group by name
having count(*) = 1;

BigQuery works quite well with aggregations so this might be quite efficient as well. BigQuery与聚合效果很好,因此这也可能非常有效。

Below is for BigQuery Standard SQL and works for any number of columns w/o explicitly calling them out and does not require any join'ing or sub-selects 以下是适用于BigQuery标准SQL的代码,适用于任意数量的列,无需显式调用它们,不需要任何联接或子选择

#standardSQL
SELECT t.*
FROM (
  SELECT ANY_VALUE(t) t
  FROM `project.dataset.table` t
  GROUP BY name
  HAVING COUNT(1) = 1
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM