这是我当前的查询:
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
答案 0 :(得分:1)
执行此操作的方法是GROUP BY
所有其他列。由于您can't group by aliased columns directly(在Oracle和大多数RDMBSes中),您必须将整个事物包装在另一个查询中并在那里进行分组。
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;
这看起来很难看,但实际上并没有产生可怕的性能影响。
答案 1 :(得分:0)
select groupcol, sum(cred)
from table1
group by rollup(groupcol)