为啥这个 sql 查询不能准确传递结果?

Posted

技术标签:

【中文标题】为啥这个 sql 查询不能准确传递结果?【英文标题】:why this sql query does not pass result accurately?为什么这个 sql 查询不能准确传递结果? 【发布时间】:2013-07-24 07:18:14 【问题描述】:

我希望sum(t1+t2) as d 结果使用group by tblpersonalInfo.applicantID 准确发布,但是此查询向我显示 d 的结果与tblpersonalInfo.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

【问题讨论】:

为什么注释掉了 sum 和 group by? 显示您遇到问题的实际查询,而不是注释掉大块的修改版本。无论如何,我怀疑问题出在DISTINCT。如果要分组,则不需要。 ...我试图编辑查询以使其更具可读性,但注释掉的部分会造成真正的混乱 tblexperience和其他表有什么关系? 从重写查询开始,将子查询粘贴到临时表中并首先检查它们的结果。最后处理查询本身。 【参考方案1】:

不是 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

【讨论】:

感谢重播..但我想对两个查询求和,例如 sum(T+T)T 是另一个值的和,另一个 T 是另一个值【参考方案2】:

您在子查询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

【讨论】:

【参考方案3】:

试试这个:

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

【讨论】:

以上是关于为啥这个 sql 查询不能准确传递结果?的主要内容,如果未能解决你的问题,请参考以下文章

MySQL中为啥不能在select语句中使用into将查询结果存入新表,但是SQL sever可以

为啥sql查询语句中的count(*)等聚合函数可以放在having后面,而不能放在where后面?

如何校验SQL查询结果是否准确

sql 左连接查询出来的求各结果不准确,被乘了2倍

mysql只能取出一条记录为啥不显示

为啥sql查询结果会打印出不需要的字符?