[英]SQL Select Unique Rows
Example1: 范例1:
IDENT | CURRENT | SOURCE
12345 | 12345 | A
23456 | 12345 | B
34567 | 12345 | C
Example2: 范例2:
IDENT | CURRENT | SOURCE
56789 | 56789 | A
Table with 3 columns, IDENT, CURRENT and SOURCE. 具有3列的表,IDENT,CURRENT和SOURCE。 I'm trying to write a query that will only display * on records where there are only one unique CURRENT record and IDENT = CURRENT (EXAMPLE 2). 我正在尝试编写一个查询,该查询仅在只有一个唯一的CURRENT记录且IDENT = CURRENT(示例2)的记录上显示*。 There are records that have the same CURRENT but different IDENT (Example 1), these records should be omitted from the results. 有些记录具有相同的CURRENT但具有不同的IDENT(示例1),这些记录应从结果中省略。
All current queries I'm trying where IDENT=CURRENT is displaying results similar to EXAMPLE 1. Not sure if I need somehow use WHERE CURRENT COUNT = 1. 我正在尝试在IDENT = CURRENT的所有当前查询中显示与示例1类似的结果。不确定是否需要使用WHERE CURRENT COUNT = 1。
select * from table
where ident=current
and current in (select current from table group by 1 having count(*)=1)
or without a subquery 或没有子查询
select
min(ident) as ident,
current, min(source) as source
from table
where ident=current
group by current
having count(*)=1
SELECT M.*
FROM Table1 AS M
WHERE M.IDENT = M.CURRENT
AND (
SELECT COUNT(*)
FROM Table1 AS S
WHERE M.IDENT = S.IDENT
AND M.CURRENT = S.CURRENT
) = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.