0

我希望sum(t1+t2) as d使用 准确发布结果group by tblpersonalInfo.applicantID,但此查询显示我的结果与 group bytblpersonalInfo.applicantID但此查询增量显示 d 的结果。

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
4

3 回答 3

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
于 2013-07-24T07:59:10.943 回答
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
于 2013-07-24T09:13:54.307 回答
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
于 2013-07-24T08:47:19.410 回答