我在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不能那样工作。当计数匹配时,而不是返回0,这将为Record_count返回null。我基本上将记录计数计算为:
record_count=count1-count2
如果计数相等则为0,否则为差。什么是正确的SQL?
答案 0 :(得分:3)
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
(SQL Server中的EXCEPT
)是一个完全不同的动物:)
如果您了解UNION
然后考虑集合,您就会理解MINUS
/ EXCEPT
答案 1 :(得分:1)
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
答案 2 :(得分:1)
我认为这可能就是你要找的东西
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