简体   繁体   English

检索唯一记录SQL

[英]Retrieve Unique records SQL

I have a table UNIQUES like : 我有一个表UNIQUES像:

ID        DESC      DATE         NUMBER        Amount
100       TEST1     01-01-18     1009674       10.20 
100       TEST2     01-02-18     1009674       245.10
100       TEST3     01-03-18     1009942       156.000  
100       TEST4     02-14-18     00042EX       154.6
100       TEST5     04-15-18     00042EX       25.10
100       TEST6     05-20-18     1011055       1564.0

And I would like to get the unique records where the Number field has not duplicate or is not repeating. 我想获得“编号”字段没有重复或没有重复的唯一记录。

Result expected: 预期结果:

ID      DESC      DATE          NUMBER         AMOUNT
100     TEST3     01-03-18      1009942        156.000 
100     TEST6     05-20-18      1011055        1564.0 

Query I'm using: 我正在使用的查询:

SELECT * FROM UNIQUES 
WHERE NUMBER NOT IN (SELECT NUMBER FROM 
UNIQUES GROUP BY NUMBER HAVING COUNT(NUMBER)=1)

Any assistance or help would be really appreciated. 任何帮助或帮助将不胜感激。

I think you are pretty close 我觉得你很亲密

SELECT * FROM UNIQUES 
WHERE NUMBER IN (SELECT NUMBER FROM 
UNIQUES GROUP BY NUMBER HAVING COUNT(NUMBER)=1)

And you want: 而你想要:

ID      DESC      DATE          NUMBER         AMOUNT
100     TEST3     01-03-18      1009942        156.000 
100     TEST6     05-20-18      1011055        1564.0 

And my answer gives: 我的回答是:

在此处输入图片说明

There are several good ways to do this, I'm just going with the simplest based upon the question by just changing it from NOT IN to IN 有几种很好的方法可以做到这一点,我只是根据问题将最简单的方法从NOT IN更改为IN

You can use subquery : 您可以使用subquery

select u.*
from uniques u
where not exists (select 1 from uniques u1 where u1.number = u.number and u1.desc <> u.desc);

I would just use window functions: 我只使用窗口函数:

select u.*
from (select u.*, count(*) over (partition by number) as cnt
      from uniques u
     ) u
where cnt = 1;
SELECT SRC.ID, SRC.DESC, SRC.DATE, SRC.NUMBER, SRC.Amount
FROM UNIQUES AS SRC

INNER JOIN (SELECT NUMBER FROM UNIQUES GROUP BY NUMBER HAVING COUNT(*)=1) AS NonDupe 
ON SRC.Number = NonDupe.Number

I would avoid using column names such as DESC, DATE and NUMBER as they occur in the list of SQL and ODBC reserved words. 我会避免使用列名,例如DESC,DATE和NUMBER,因为它们出现在SQL和ODBC保留字列表中。

Aside from the not exists method, you can outer join the table to itself, and check for nulls in the "duplicate" table. 除了not exists方法之外,您还可以将表outer join到自身,并在“重复”表中检查是否为空。

select *
from
    uniques u
    left outer join uniques dup
        on (u.NUMBER=dup.NUMBER)
where
    dup.NUMBER is null

This works because the left outer join will return everything from the "left" table (aliased to u above), and it will return null values from the "right" table (aliased to dup above) if there is no row found with the join on criteria. 之所以有效,是因为如果没有在join on找到任何行,则左外部联接将返回“ left”表中的所有内容(别名为上面的u ),并且它将返回“ right”表中的null值(别名为dup )。 join on标准。

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

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