首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅在窗口分区中查找值不重复的行。

仅在窗口分区中查找值不重复的行。
EN

Stack Overflow用户
提问于 2020-07-30 20:45:50
回答 4查看 40关注 0票数 0

我想看看为什么某些descriptions对于相同的permit id是不同的。这是桌子(我用的是雪花):

代码语言:javascript
复制
create or replace table permits (permit varchar(255), description varchar(255));

// dupe permits, dupe descriptions, throw out
INSERT INTO permits VALUES ('1', 'abc'); 
INSERT INTO permits VALUES ('1', 'abc');

// dupe permits, unique descriptions, keep
INSERT INTO permits VALUES ('2', 'def1'); 
INSERT INTO permits VALUES ('2', 'def2');
INSERT INTO permits VALUES ('2', 'def3');

// dupe permits, unique descriptions, keep
INSERT INTO permits VALUES ('3', NULL);   
INSERT INTO permits VALUES ('3', 'ghi1');

// unique permit, throw out
INSERT INTO permits VALUES ('5', 'xyz'); 

我想要的是查询这个表,只获取具有重复I但有不同描述的行集。

我想要的输出是:

代码语言:javascript
复制
+---------+-------------+
| PERMIT  | DESCRIPTION |
+---------+-------------+
|       2 | def1        |
|       2 | def2        |
|       2 | def3        |
|       3 |             |
|       3 | ghi1        |
+---------+-------------+

我试过这个:

代码语言:javascript
复制
with with_dupe_counts as (
    select
        count(permit) over (partition by permit order by permit) as permit_dupecount,
        count(description) over (partition by permit order by permit) as description_dupecount,
        permit,
        description
    from permits
)
select *
from with_dupe_counts
where permit_dupecount > 1 
and description_dupecount > 1

这给了我许可1和2,并计算了它们是否是唯一的描述:

代码语言:javascript
复制
+------------------+-----------------------+--------+-------------+
| PERMIT_DUPECOUNT | DESCRIPTION_DUPECOUNT | PERMIT | DESCRIPTION |
+------------------+-----------------------+--------+-------------+
|                2 |                     2 |      1 | abc         |
|                2 |                     2 |      1 | abc         |
|                3 |                     3 |      2 | def1        |
|                3 |                     3 |      2 | def2        |
|                3 |                     3 |      2 | def3        |
+------------------+-----------------------+--------+-------------+

我认为会有用的是

代码语言:javascript
复制
count(unique description) over (partition by permit order by permit) as description_dupecount

但正如我所意识到的,有很多东西在窗口函数中不起作用。这个问题不一定是“如何使计数(唯一x)在窗口函数中工作”,因为我不知道这是否是解决这个问题的最佳方法。

我认为一个简单的group by将无法工作,因为我想要返回原始行。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-07-30 20:51:14

一种方法是使用min()max()count()

代码语言:javascript
复制
select *
from (select p.*,
             min(description) over (partition by permit) as min_d,
             max(description) over (partition by permit) as max_d,
             count(description) over (partition by permit) as cnt_d,
             count(*) over (partition by permit) as cnt,
            count(permit) over (partition by permit order by permit) as permit_dupecount
      from permits
     )
where min_d <> max_d or cnt_d <> cnt;
票数 1
EN

Stack Overflow用户

发布于 2020-07-30 20:51:59

我只会用exists

代码语言:javascript
复制
select p.*
from permits p
where exists (
    select 1 
    from permits p1 
    where p1.permit = p.permit and p1.description <> p.description
)

要处理null值,我们可以使用标准的空安全相等操作符IS DISTINCT FROM,它支持:

代码语言:javascript
复制
select p.*
from permits p
where exists (
    select 1 
    from permits p1 
    where 
        p1.permit = p.permit 
        and p1.description is distinct from p.description
)
票数 1
EN

Stack Overflow用户

发布于 2020-07-30 20:51:29

应起作用

代码语言:javascript
复制
SELECT DISTINCT p1.permit, p1.description
FROM permits p1
JOIN permits p2 ON p1.permit = p2.permit
WHERE p1.description != p2.description OR p1.description IS NULL AND p2.description IS NOT NULL
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63181132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档