【问题标题】:Adding up the rows of a column in a query在查询中添加列的行
【发布时间】:2011-11-16 14:55:41
【问题描述】:

这是我当前的查询:

SELECT sac.cred, s.status, (SELECT NVL (csl.census_dates, tl.census_dates) 
                                         FROM  schema.sections cs,  schema.sections_ls csl,  schema.terms tl 
                                        WHERE cs.course_sections_id = csl.course_sections_id(+)AND csl.pos(+) = 1 AND cs.term = tl.terms_id 
                                          AND tl.pos = 1 AND cs.course_sections_id = cs2.course_sections_id AND ROWNUM = 1)AS censusDate, 
                                      (SELECT NVL (p.ssn, 'xxx-xx-xxxx') FROM   schema.person p 
                                        WHERE p.id = sac.person_id) AS ssn, 
                                       //schema.person_name(sac.person_id, 'FML') as fml, 
                                       //schema.person_name(sac.person_id, 'LF') as lf 
                                 FROM  schema.student_acad_cred sac JOIN  schema.statuses s 
                                   ON s.student_acad_cred_id = sac.student_acad_cred_id 
                                 JOIN  schema.terms tl ON sac.term = tl.terms_id 
                                 JOIN  schema.student_course_sec scs ON sac.student_course_sec = scs.student_course_sec_id 
                                 JOIN  schema.course_sections cs2 ON scs.course_section = cs2.course_sections_id 
                                 JOIN  schema.terms t ON tl.terms_id = t.terms_id 
                                WHERE sac.person_id = '1111111111' 
                                  AND (s.status IN ('A', 'N') OR (s.status = 'D' AND final_grade IS NOT NULL)) 
                                  AND s.pos = '1'AND tl.pos = '1' AND tl.terms_id = 'spring'; 

结果如下:

cred      status   currentDate   censusDate     ssn
====      ======   ===========   ==========     ===
  3         N       11/16/2011   12/15/2011      xxx-xx-xxxx
  4         N       11/16/2011   12/15/2011      xxx-xx-xxxx
  3         N       11/16/2011   12/15/2011      xxx-xx-xxxx
  4         N       11/16/2011   12/15/2011      xxx-xx-xxxx
  1         N       11/16/2011   12/15/2011      xxx-xx-xxxx

好的,我要做的是使用 sum()(或其他一些函数)将所有被拉取的学时加起来。因此,在这种情况下,所有信用小时数的总和将为“15”。有没有办法在查询中做到这一点?理想情况下,我想要这样的东西:

cred      status   currentDate   censusDate     ssn
====      ======   ===========   ==========     ===
 15         N       11/16/2011   12/15/2011      xxx-xx-xxxx

【问题讨论】:

  • 为什么要显示SUM 个人记录的信用信息?此外,当您基于不变的person_id 获得ssn 时,为什么ssn 对于同一个人会有所不同?事实上,你为什么不直接 JOINschema.person 而不是做一个子选择?
  • 我真的需要理由吗?大声笑,毫无疑问,需要总学时。结果完全是虚构的,我只是在打字。加入人员将需要 3 次以上的连接(我没有设置表),这使得查询的执行时间大约是原来的两倍。
  • 还有 3 个加入?它只需要一个连接:JOIN schema.person p ON sac.person_id = p.person_id。你基本上已经在这样做了,只是以一种非常奇怪的方式。我问的原因是因为你的输出没有任何意义——ssn 永远不会不同,它总是一样的。在我们确定了这一事实之后,很容易看到您只是输出同一行 5 次......这很愚蠢。相反,您应该执行 GROUP BY 并且只生成一行,这就是 SUM 有意义的地方。
  • 好的,我很抱歉,我误解了你之前的评论,但你对加入的看法是对的,我可以解决这个问题。就 ssn 而言,我只是将 # 扔在那里,它们将始终完全相同,现在已修复。我想我要问的部分是使用 sum 和 group by 来获得一行。
  • 问题已编辑,感谢您的提示。

标签: sql oracle sum


【解决方案1】:

执行此操作的方法是 GROUP BY 您的所有其他列。由于您 can't group by aliased columns directly(在 Oracle 和大多数 RDMBS 中),您必须将整个事情包装在另一个查询中并在那里进行分组。

SELECT SUM(cred), status, censusDate, ssn 
FROM
    (SELECT sac.cred, s.status, 
          (SELECT NVL (csl.census_dates, tl.census_dates) 
             FROM  schema.sections cs,  schema.sections_ls csl,  schema.terms tl 
            WHERE cs.course_sections_id = csl.course_sections_id(+)AND csl.pos(+) = 1 AND cs.term = tl.terms_id 
              AND tl.pos = 1 AND cs.course_sections_id = cs2.course_sections_id AND ROWNUM = 1)AS censusDate, 
          (SELECT NVL (p.ssn, 'xxx-xx-xxxx') FROM   schema.person p 
            WHERE p.id = sac.person_id) AS ssn, 
           //schema.person_name(sac.person_id, 'FML') as fml, 
           //schema.person_name(sac.person_id, 'LF') as lf 
     FROM  schema.student_acad_cred sac JOIN  schema.statuses s 
       ON s.student_acad_cred_id = sac.student_acad_cred_id 
     JOIN  schema.terms tl ON sac.term = tl.terms_id 
     JOIN  schema.student_course_sec scs ON sac.student_course_sec = scs.student_course_sec_id 
     JOIN  schema.course_sections cs2 ON scs.course_section = cs2.course_sections_id 
     JOIN  schema.terms t ON tl.terms_id = t.terms_id 
    WHERE sac.person_id = '1111111111' 
      AND (s.status IN ('A', 'N') OR (s.status = 'D' AND final_grade IS NOT NULL)) 
      AND s.pos = '1'AND tl.pos = '1' AND tl.terms_id = 'spring')
GROUP BY status, censusDate, ssn;

这看起来很难看,但实际上并没有对性能产生可怕的影响。

【讨论】:

    【解决方案2】:
    select groupcol, sum(cred)
    from table1
    group by rollup(groupcol)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 2019-08-26
      相关资源
      最近更新 更多