为什么这个SQL查询没有准确传递结果?

时间:2013-07-24 07:18:15

标签: sql sql-server sql-server-2008

我希望使用sum(t1+t2) as d准确发布group by tblpersonalInfo.applicantID结果,但此查询显示的是tblpersonalInfo.applicantID分组的结果,但此查询会逐步显示结果。

SELECT  DISTINCT 

    t1+t2,

    tblPersonalInfo.ApplicantId,
    tblPersonalInfo.Applicantname
FROM    (
    SELECT  SUM(tblExpeRange.score)as t2
    FROM    tblexperience
    LEFT OUTER JOIN tblExpeRange 
        ON tblexperience.ExpRange=tblExpeRange.expeRange

    GROUP BY tblexperience.ApplicantId
    ) tblexperience,
    ( 
    SELECT  SUM(tblGradPoint.score) AS t1
    FROM    tblAcademicInfo
        LEFT OUTER JOIN tblGradPoint 
            ON tblAcademicInfo.cgpa=tblGradPoint.[cgpa/division]
    GROUP BY tblAcademicInfo.ApplicantId
    ) tblAcademicInfo,
    tblPersonalInfo

    INNER JOIN tblCircular 
        ON tblPersonalInfo.Cirname = tblCircular.Cirname
WHERE tblCircular.Cirname=(tblPersonalInfo.Cirname)

RETURN

3 个答案:

答案 0 :(得分:0)

不是100%确定您的所有查询,但听起来您可能需要将两个子查询联合为一个,然后您可以在SELECT子句中求和...

SELECT  sum(T) AS d,
        Y.ApplicantId,
        Y.Applicantname
FROM    (
        SELECT  tblexperience.ApplicantId,
                SUM(tblExpeRange.score)as T
        FROM    tblexperience
                LEFT OUTER JOIN tblExpeRange 
                  ON tblexperience.ExpRange=tblExpeRange.expeRange
        GROUP BY tblexperience.ApplicantId
        UNION 
        SELECT  tblAcademicInfo.ApplicantId,
                SUM(tblGradPoint.score) AS T
        FROM    tblAcademicInfo
                LEFT OUTER JOIN tblGradPoint 
                  ON tblAcademicInfo.cgpa=tblGradPoint.[cgpa/division]
        GROUP BY tblAcademicInfo.ApplicantId
        ) X
        INNER JOIN tblPersonalInfo Y
            ON X.ApplicantId = Y.ApplicantId 
        INNER JOIN tblCircular Z
            ON Y.Cirname = Z.Cirname  --(*)
--WHERE Z.Cirname=(Y.Cirname)   --.....maybe not required as the line above (*) deals with this
GROUP BY Y.ApplicantId,
        Y.Applicantname

答案 1 :(得分:0)

子查询tblAcademicInfotblexperience之间没有任何关联,因此您创建了一个CROSS JOIN,其中第一个值与第二个值中的每个值相加。也许您应该尝试将ApplicantId添加到每个子查询选择列表中,并使用它在它们之间加入tblPersonalInfo

SELECT  DISTINCT 

    t1+t2,

    tblPersonalInfo.ApplicantId,
    tblPersonalInfo.Applicantname
FROM    
    (
        SELECT  
            tblexperience.ApplicantId
           ,SUM(tblExpeRange.score)as t2
        FROM tblexperience
        LEFT OUTER JOIN tblExpeRange 
           ON tblexperience.ExpRange=tblExpeRange.expeRange
        GROUP BY tblexperience.ApplicantId
    ) tblexperience,
    INNER JOIN
    ( 
        SELECT  
             tblAcademicInfo.ApplicantId
            ,SUM(tblGradPoint.score) AS t1
        FROM tblAcademicInfo
        LEFT OUTER JOIN tblGradPoint 
            ON tblAcademicInfo.cgpa=tblGradPoint.[cgpa/division]
        GROUP BY tblAcademicInfo.ApplicantId
    ) tblAcademicInfo ON tblAcademicInfo.ApplicantId = tblexperience.ApplicantId
    INNER JOIN tblPersonalInfo ON tblexperience.ApplicantId = tblPersonalInfo.ApplicantId 
    INNER JOIN tblCircular 
        ON tblPersonalInfo.Cirname = tblCircular.Cirname

编辑:另外,如果您使用公用表表达式和别名,您是否认为此查询看起来更具可读性:

WITH CTE_Experience AS 
(
    SELECT  
        e.ApplicantId
        ,SUM(er.score) AS t2
    FROM tblexperience e
        LEFT JOIN tblExpeRange er ON e.ExpRange = er.expeRange
    GROUP BY e.ApplicantId
)
,CTE_AcademicInfo AS 
(
    SELECT  
         ai.ApplicantId
        ,SUM(gp.score) AS t1
    FROM tblAcademicInfo ai
        LEFT JOIN tblGradPoint gp ON ai.cgpa = gp.[cgpa/division]
    GROUP BY ai.ApplicantId
)
SELECT
    ai.t1 + ex.t2,
    pin.ApplicantId,
    pin.Applicantname
FROM tblPersonalInfo pin 
    INNER JOIN CTE_Experience ex ON ex.ApplicantId = pin.ApplicantId
    INNER JOIN CTE_AcademicInfo ai ON ai.ApplicantID = pin.ApplicantId
    INNER JOIN tblCircular ci ON ci.Cirname = pin.Cirname

答案 2 :(得分:0)

试试这个:

SELECT
   tblPersonalInfo.ApplicantId,
   tblPersonalInfo.ApplicantName,
   (tblExperience.t2+tblAcademicInfo.t1) as d
FROM tblPersonalInfo
INNER JOIN (
   SELECT SUM(tblExpeRange.score) as t2,
       tblExperience.ApplicantId
   FROM tblExperience
   LEFT OUTER JOIN tblExpeRange
       ON tblExperience.ExpRange=tblExpeRange.ExpRange
   GROUP BY tblExperience.ApplicantId
)tblExperience ON tblPersonalInfo.ApplicantId=tblExperience.ApplicantId
INNER JOIN (
   SELECT SUM(tblGradPoint.score) as t1,
       tblAcademicInfo.ApplicantId
   FROM tblAcademicInfo
   LEFT OUTER JOIN tblGradPoint
       ON tblAcademicInfo.cgpa=tblGradPoint.[cgpa/division]
   GROUP BY tblAcademicInfo.ApplicantId
)tblAcademicInfo ON tblPersonalInfo.ApplicantId=tblAcademicInfo.ApplicantId
INNER JOIN tblCircular
   ON tblPersonalInfo.Cirname=tblCircular.Cirname