SQL Server中的聚合函数相关问题

时间:2011-07-14 11:59:11

标签: sql sql-server-2008-r2

我何时使用以下查询:

SELECT  
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    (SELECT COUNT (*) FROM tbUS_ReferJob WHERE iJobId_FK = 202424 AND sReferredFrom = 'F' AND iUserId_FK = 9550) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

它给出了正确的结果。

JobID   RowNumber   FaceBook_Applications
202424  1           2

但是当我使用下面的查询时

SELECT
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    SUM(CASE WHEN RJ.sReferredFrom = 'F' THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

提供的结果不正确

JobID   RowNumber   FaceBook_Applications
202424  1           12

现在我的问题是

  1. 背后的原因是什么?
  2. 如何使用SUM()功能降低成本呢?
  3. 任何建议都会有所帮助。

    感谢您的时间。

    更新

    SELECT  AC.iJobID as JobID,
            ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
            SUM(CASE WHEN (RJ.sReferredFrom = 'F' AND RJ.iJobId_FK = 202424 AND RJ.iUserId_FK = 9550) THEN 1 ELSE 0 END) AS FaceBook_Applications
    FROM    tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
    WHERE   AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
    GROUP BY AC.iJobID
    

    它正在给予:

    JobID   RowNumber   FaceBook_Applications
    202424  1            8
    

    更新2

    SELECT iJobID_FK, sReferredFrom FROM tbUS_ReferJob WHERE iUserID_FK=9550 AND iJobID_FK=202424 AND sReferredFrom='F'
    

    结果:

    iJobID_FK   sReferredFrom
    202424          F
    202424          F
    

3 个答案:

答案 0 :(得分:1)

在第一个查询中,您将子查询限制为:

WHERE iUserId_FK = 9550

第二个查询中缺少此限制。 它应该是这样的:

SELECT  AC.iJobID as JobID,
        ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
        SUM(CASE WHEN RJ.sReferredFrom = 'F' THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM    tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE   AC.iJobID = RJ.iJobId_FK 
AND     AC.iJobID = 202424 
AND     RJ.iUserId_FK = 9550

答案 1 :(得分:1)

如果我正确查看,我认为您在第二个查询中缺少过滤器

AND iUserId_FK = 9550

第二个查询应如下所示:

SELECT
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    SUM(CASE WHEN RJ.sReferredFrom = 'F'  AND iUserId_FK = 9550 THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

修改

我认为连接条件可能存在一些问题...尝试在求和之前进行更清晰的连接

SELECT
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    SUM(CASE WHEN RJ.sReferredFrom = 'F'   THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC
JOIN tbUS_ReferJob RJ
ON AC.iJobID =  RJ.iJobId_FK AND Rj.iUserId_FK = 9550
WHERE AC.iJobID = 202424
GROUP BY AC.iJobID

编辑2

SELECT
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    COUNT(*) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC
JOIN tbUS_ReferJob RJ
ON AC.iJobID =  RJ.iJobId_FK AND Rj.iUserId_FK = 9550 AND RJ.sReferredFrom = 'F'
WHERE AC.iJobID = 202424
GROUP BY AC.iJobID

答案 2 :(得分:1)

进行第二次查询(我将在此重复):

SELECT
    AC.iJobID as JobID,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,     
    SUM(CASE WHEN RJ.sReferredFrom = 'F' THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

并删除(或注释掉)所有分组&聚合(排名也是如此,因为它对我的建议毫无用处),例如:

SELECT
    AC.iJobID as JobID, RJ.iUserId_FK,
    ROW_NUMBER()OVER (ORDER BY AC.iJobID) AS RowNumber,
    SUM(CASE WHEN RJ.sReferredFrom = 'F' THEN 1 ELSE 0 END) AS FaceBook_Applications
FROM tbUS_AffiliateJobCount AC,tbUS_ReferJob RJ
WHERE AC.iJobID = 202424 AND RJ.iJobId_FK = 202424
GROUP BY AC.iJobID

现在运行生成的查询,看看它产生了多少F个。我的观点是,连接的结果很可能包含许多重复项,因为两个表的子集都返回多行并且导致旧的Cartesian product。解决方案可能包括清理原始表中的重复项(如果适用),或者使WHERE条件更具体。