检查记录是否存在于前一个日期而不是当前日期在相同的 table 和 return 记录匹配和不匹配的计数

check if record exists in previous date and not in current date in the same table and return count of records matched and unmatched

我正在尝试获取一个日期和另一个日期的记录数。什么是最有效的方法?

id date
AB 6/11/2021
AB 6/11/2021
BC 6/04/2021
BC 6/04/2021
AB 6/04/2021
AB 6/04/2021

这应该 return True =2(Ab 出现在 04/21)和 False=2

每个 ID 如果它在多个其他日期是 COUNT DISTINCT

SELECT id, count(distinct date) > 1
FROM VALUES
    ('AB', '6/11/2021'),
    ('AB', '6/11/2021'),
    ('BC', '6/04/2021'),
    ('BC', '6/04/2021'),
    ('AB', '6/04/2021'),
    ('AB', '6/04/2021')
    t(id,date)
group by 1

给出:

ID COUNT(DISTINCT DATE) > 1
AB TRUE
BC FALSE

因此处于此状态的事物数为:

select count_if(c)
from (
    SELECT id, count(distinct date) > 1 as c
    FROM VALUES
        ('AB', '6/11/2021'),
        ('AB', '6/11/2021'),
        ('BC', '6/04/2021'),
        ('BC', '6/04/2021'),
        ('AB', '6/04/2021'),
        ('AB', '6/04/2021')
        t(id,date)
    group by 1
)

给予:

COUNT_IF(C)
1

如果您想以 table:

的形式查看
create table data as 
select * FROM VALUES
        ('AB', '6/11/2021'),
        ('AB', '6/11/2021'),
        ('BC', '6/04/2021'),
        ('BC', '6/04/2021'),
        ('AB', '6/04/2021'),
        ('AB', '6/04/2021')
        t(id,date);
select count_if(c)
from (
    SELECT id, count(distinct date) > 1 as c
    FROM data
    group by 1
)

选择ID可以放在上面:

select count_if(c)
from (
    SELECT count(distinct date) > 1 as c 
    FROM data 
    group by id
);

啊,我想你要的是评论:

select c, count(*)
from (
    SELECT count(distinct date) > 1 as c 
    FROM data 
    group by id
)
group by 1;
C COUNT(*)
TRUE 1
FALSE 1

另一种思考方式:

select id, 
    date,
    c as count_same,
    count(date) over (partition by id) > 1 as in_many
from (
SELECT id, date, count(*) as c
FROM data 
group by 1,2
)

给出:

ID DATE COUNT_SAME IN_MANY
AB 6/11/2021 2 TRUE
BC 6/04/2021 2 FALSE
AB 6/04/2021 2 TRUE

但是如果您只需要每个 ID 一行,并且要知道它们是否在多个日期中,这很简单,但是您希望 id/date 计算这么多行的事实是怎么回事,折叠起来时。

所需输出的描述相当模糊..