SQL

时间:2016-09-04 08:05:10

标签: sql oracle group-by

我正在对单一工作进行一些修改,我们正在处理一个简单场景的数据仓库练习,数据仓库正在构建,以计算学生根据不同时间段使用计算机实验室的次数,学期和专业。 作为参考,我们的数据仓库由四个原始表构建:

  • USELOG(log_date,log_time,Student_ID,Act)
  • 学生(Student_ID,性别,完整/部分,类型,Class_ID,Major_Code)
  • Major(Major_name,Major_code)Major(Major_name,Major_code)
  • Class(Class_description,Class_ID)

我得到了两个错误,一个我从(我认为)工作过的错误,另一个是一个错误的组,我似乎无法解决,因为我有老师的解决方案,我们有类似的分组,但我想有的是关于我如何完成任务的不同之处。

无论如何,我的代码如下:

Create table Period_DIM (
PeriodID number,
PeriodDesc varchar2(15),
begin_time date,
end_time date);

Create Table Semester_DIM(
SemesterID number,
Semester_Desc varchar2(15),
start_date date,
end_date date);

Create Table Major_DIM as Select * from major;

Create Table class_dim as select * from class;

--Populate Period_Dim
Insert Into Period_DIM Values(1, 'Morning', To_date('06:01', 'HH24:MI'), To_date('12:00', 'HH24:MI'));
Insert Into Period_DIM Values(2, 'Afternoon', To_date('12:01', 'HH24:MI'), To_date('18:00', 'HH24:MI'));
Insert Into Period_DIM Values(3, 'Evening', To_date('18:01', 'HH24:MI'), To_date('06:00', 'HH24:MI'));

--Populate Semester_DIM
Insert Into Semester_DIM Values (1, 'Semester 1', To_date('01-Jan', 'DD-MON'), To_date('15-JUL', 'DD-MON'));
Insert Into Semester_DIM Values (2, 'Semester 2', To_date('16-Jul', 'DD-MON'), To_date('31-DEC', 'DD-MON'));

--Create Temp Fact Table
Create table tempfacttable As Select u.LOG_DATE, u.LOG_TIME, u.STUDENT_ID, s.CLASS_ID, s.MAJOR_CODE
From Uselog u, student s
where u.STUDENT_ID = s.STUDENT_ID;

--Add a timeID to each row in the table because the data doesn't originally have them
alter table tempfacttable add (timeid number);
update tempfacttable
set timeid = 1
where to_char(log_time, 'HH24:MI') >= '06:01'
and to_char(log_time, 'HH24:MI') <= '12:00';

update tempfacttable
set timeid = 2
where to_char(log_time, 'HH24:MI') >= '12:01'
and to_char(log_time, 'HH24:MI') <= '18:00';

--Use OR in this case
update tempfacttable
set timeid = 3
where to_char(log_time, 'HH24:MI') >= '18:00'or to_char (log_time, 'HH24:MI') <= '06:00';

--Add a semester ID based on the date in the tempfact cause its not contained in the Dimensions
alter table tempfacttable add (semid varchar2(10));
update tempfacttable
set semid= 'S1'
where to_char(log_date, 'MMDD') >= '0101'
and to_char(log_date, 'MMDD') <= '0715';

update tempfacttable
set semid = 'S2'
where to_char(log_date, 'MMDD') >= '0716'
and to_char(log_date, 'MMDD') <= '1231';

--Create the Fact Table
Create table factTable as 
select t.SEMID, t.TIMEID, t.MAJOR_CODE, t.CLASS_ID, count(t.student_id) as total_usage
From tempfacttable t
group by t.semid, t.timeid, t.class_id, t.major_code;

select total_usage from tempfacttable; --returns error saying total_usage is an invalid operator

--Usage for time period by major and by student's class
--Select t.timeid, p.perioddesc, m.major_code, m.major_name, c.class_id, c.class_description, sum(t.total_usage) as usage_numbers 
--Previous line returns error because of t.total_usage, workaround is in the next line
Select t.timeid, p.PERIODDESC, m.MAJOR_CODE, m.MAJOR_NAME, c.CLASS_ID, c.CLASS_DESCRIPTION, sum(count(t.student_id)) as usage_numbers
from tempfacttable t, period_dim p, class_dim c, major_dim m
where t.timeid = p.PERIODID
and t.major_code = m.major_code
group by t.timeid, p.PERIODDESC, t.major_code, m.major_name, t.class_id, c.class_description;

我似乎无法使其正常工作,我收到一条错误消息,指出'不是单一的群组功能'。我真的很感激这方面的一些帮助以及为什么我不能引用total_usage的原因,即使我从包含它的表中选择*它也会显示其余的属性。非常感谢你

1 个答案:

答案 0 :(得分:1)

我认为这是因为您没有在total_usage中定义tempfacttable列。但是,您的total_usage

中有一列factTable

请改为尝试:

select total_usage from factTable;