如何解决SQL - 雪花减号运算符
嗨,我正在运行一个查询来检查两个日期之间表中的任何更改....
SELECT * FROM TABLE_A where run_time = current_date()
MINUS
SELECT * FROM TABLE_A where run_time = current_date()-1
第一个 select 语句(其中 run_time = current_date() 返回 3,357,210 条记录。 第二条select语句(其中run_time = current_date()-1返回0条记录。
使用 MINUS 运算符,我期望看到 3,210 条记录 (3,210 - 0),但结果却是 2,026,434
有什么想法吗?谢谢
解决方法
https://docs.snowflake.com/en/sql-reference/operators-query.html#minus-except
从一个查询的结果集中删除出现在另一个查询结果集中的行,消除重复。
因此,您在第一个查询中只有 2,026,434 个唯一值。丢失的百万分之一是重复项,已被消除。
,这个查询:
SELECT * FROM TABLE_A where run_time = current_date()
MINUS
SELECT * FROM TABLE_A where run_time = current_date()-1
将始终返回来自 Table_A
的所有唯一行。为什么?因为 run_time
是列之一并且在两个查询中是不同的。 MINUS
查看所有列。请注意,即使第二个查询返回行也是如此,因为行上的值不同。
如果您的总数与总行数不同,则表中有重复项。
这里有两种获取新记录的方法。让我假设相同的记录由 col1
/col2
标识:
select col1,col2
from table_a
where run_time in (current_date(),current_date() -1)
group by col1,col2
having min(run_time) = current_date();
也就是说,第一次出现的是当前日期。
或者:
select col1,col2
from table_a a
where a.run_time = current_date() and
not exists (select 1
from table_a a2
where a2.run_time = current_date() - 1 and
a2.col1 = a.col1 and a2.col2 = a.col2
);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。