在SQL中获取两个计数的差异

发布于 2024-12-10 19:44:35 字数 484 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

末が日狂欢 2024-12-17 19:44:35
SELECT
(
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
)  -
(
SELECT COUNT(*)
FROM db..EXT_ACXIOM_WUL_FILE  A
) TotalCount

Oracle 的 MINUS (EXCEPT< /a> 在 SQL Server 中)是完全不同的动物:)

如果您理解 UNION 然后思考集合,您就会理解 MINUS / EXCEPT

SELECT
(
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
)  -
(
SELECT COUNT(*)
FROM db..EXT_ACXIOM_WUL_FILE  A
) TotalCount

Oracle's MINUS (EXCEPT in SQL Server) is a whole different animal :)

If you understand UNION and then think sets, you will understand MINUS / EXCEPT

旧人哭 2024-12-17 19:44:35

MINUS 是设置差值,不适用于算术运算。

你可以这样做

SELECT COUNT(*) - (SELECT COUNT(*)
                   FROM   db..EXT_ACXIOM_WUL_FILE A) AS Val
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  

或其他选择

SELECT COUNT(*) - COUNT(DISTINCT A.PrimaryKey) AS Val
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 is set difference, not for arithmetic operations.

You could do

SELECT COUNT(*) - (SELECT COUNT(*)
                   FROM   db..EXT_ACXIOM_WUL_FILE A) AS Val
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  

Or another option

SELECT COUNT(*) - COUNT(DISTINCT A.PrimaryKey) AS Val
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  
相思故 2024-12-17 19:44:35

我想这可能就是您正在寻找的

    SELECT COUNT(distinct(CURRENTLY_OPTED_IN_FL + F_EMAIL.MID_KEY)) - count(distinct(EXT_ACXIOM_WUL_FILE.MID_KEY))
    FROM EXT_ACXIOM_WUL_FILE 
    LEFT OUTER JOIN F_EMAIL
      ON JOIN F_EMAIL.MID_KEY = EXT_ACXIOM_WUL_FILE.MID_KEY

I think this may be what you are looking for

    SELECT COUNT(distinct(CURRENTLY_OPTED_IN_FL + F_EMAIL.MID_KEY)) - count(distinct(EXT_ACXIOM_WUL_FILE.MID_KEY))
    FROM EXT_ACXIOM_WUL_FILE 
    LEFT OUTER JOIN F_EMAIL
      ON JOIN F_EMAIL.MID_KEY = EXT_ACXIOM_WUL_FILE.MID_KEY
影子是时光的心 2024-12-17 19:44:35

在 Oracle 数据库上,扩展上面 Adriano Carneiro 的答案,应该可以解决问题:

SELECT (
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
)
-
(SELECT COUNT(*)
FROM db..EXT_ACXIOM_WUL_FILE  A)
CustomVarName from DUAL;

ps
减号只是数学中集合的标准运算

On Oracle db, extending the answer by Adriano Carneiro above, should do the trick:

SELECT (
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
)
-
(SELECT COUNT(*)
FROM db..EXT_ACXIOM_WUL_FILE  A)
CustomVarName from DUAL;

ps
MINUS is just the standard operation for Sets in Math

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文