在SQL中获取两个计数的差异
我正在 Netezza 中进行一些 QA,我需要比较两个单独的 SQL 语句的计数。这是我当前正在使用的 SQL
SELECT COUNT(*) AS RECORD_COUNT
FROM db..EXT_ACXIOM_WUL_FILE A
LEFT JOIN (select distinct CURRENTLY_OPTED_IN_FL,mid_key from db..F_EMAIL) B
ON A.MID_KEY=B.MID_KEY
MINUS
SELECT COUNT(*)
FROM db..EXT_ACXIOM_WUL_FILE A
但是,看起来 MINUS 并不像那样工作。当计数匹配时,Record_count 将返回 null,而不是返回 0。我基本上将记录计数计算为:
record_count=count1-count2
因此,如果计数相等,则为 0,否则为差值。正确的 SQL 是什么?
I'm doing some QA in Netezza and I need to compare the counts from two separate SQL statements. This is the SQL that I am currently using
SELECT COUNT(*) AS RECORD_COUNT
FROM db..EXT_ACXIOM_WUL_FILE A
LEFT JOIN (select distinct CURRENTLY_OPTED_IN_FL,mid_key from db..F_EMAIL) B
ON A.MID_KEY=B.MID_KEY
MINUS
SELECT COUNT(*)
FROM db..EXT_ACXIOM_WUL_FILE A
However, it seems like MINUS doesn't work like that. When the counts match, instead of returning 0, this will return null for Record_count. I basically the record count to be computed as:
record_count=count1-count2
So it is 0 if the counts are equal or the difference otherwise. What is the correct SQL for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Oracle 的
MINUS
(EXCEPT
< /a> 在 SQL Server 中)是完全不同的动物:)如果您理解
UNION
然后思考集合,您就会理解MINUS
/EXCEPT
Oracle's
MINUS
(EXCEPT
in SQL Server) is a whole different animal :)If you understand
UNION
and then think sets, you will understandMINUS
/EXCEPT
MINUS
是设置差值,不适用于算术运算。你可以这样做
或其他选择
MINUS
is set difference, not for arithmetic operations.You could do
Or another option
我想这可能就是您正在寻找的
I think this may be what you are looking for
在 Oracle 数据库上,扩展上面 Adriano Carneiro 的答案,应该可以解决问题:
ps
减号只是数学中集合的标准运算
On Oracle db, extending the answer by Adriano Carneiro above, should do the trick:
ps
MINUS is just the standard operation for Sets in Math