按子查询中的多个列进行分组

时间:2015-04-16 16:00:14

标签: sql oracle group-by

所以我的查询:

SELECT a.term_id, 
       a.update_date, 
       a.campus, 
       a.last_hour, 
       a.enrolled_this_hour,
--      (SELECT SUM(a.enrolled_this_hour)
--      FROM term_enrollment_by_hour h3
--        WHERE a.term_id = h3.term_id
--          AND h3.update_date = a.update_date
--     GROUP BY h3.campus
--      ) as attempt      
  FROM (SELECT h1.term_id,
            update_date,
            this_hour,
            campus,
            last_hour,
            enrolled_this_hour
       FROM term_enrollment_by_hour h1
       LEFT OUTER JOIN
            term_enrollment_by_hour h2
         ON h1.term_id = h2.term_id
        AND h1.campus = h2.campus
ORDER BY h1.update_date, h1.update_hour, h1.campus) a;

返回类似于此的数据集:

term update_date campus last_hour enrolled 14FALL 10/03/14 WEST 06:00 15
14FALL 10/03/14 NORT 06:00 5 14FALL 10/03/14 EAST 06:00 30 14FALL 10/03/14 WEST 07:00 15 14FALL 10/03/14 NORT 07:00 5 14FALL 10/03/14 EAST 07:00 30 14FALL 10/04/14 WEST 08:00 20 14FALL 10/04/14 NORT 08:00 10 14FALL 10/04/14 EAST 08:00 50 14FALL 10/04/14 WEST 08:00 20 14FALL 10/04/14 NORT 08:00 10 14FALL 10/04/14 EAST 08:00 50

如果可能的话,我想添加一个按天和校园分组的列,但似乎无法正确汇总。以下是我希望数据的样子:

term update_date campus last_hour enrolled group_attempt 14FALL 10/03/14 WEST 06:00 15 30 14FALL 10/03/14 NORT 06:00 5 10 14FALL 10/03/14 EAST 06:00 30 60 14FALL 10/03/14 WEST 07:00 15 30 14FALL 10/03/14 NORT 07:00 5 10 14FALL 10/03/14 EAST 07:00 30 60 14FALL 10/04/14 WEST 08:00 20 40 14FALL 10/04/14 NORT 08:00 10 20 14FALL 10/04/14 EAST 08:00 50 100 14FALL 10/04/14 WEST 08:00 20 40 14FALL 10/04/14 NORT 08:00 10 20 14FALL 10/04/14 EAST 08:00 50 100

提前感谢您的帮助,如果需要更多信息,我很乐意提供。

0 个答案:

没有答案