我可以参考这张桌子吗?

时间:2019-04-11 21:12:55

标签: sql

我正在尝试显示按月,然后按导师ID排序的每个导师的已付金额。我的第一部分是正确的,可以按月份排序,但不能按导师ID排序,因为它来自其他表。

这是我的表的脚本:

create table match_history
(match_id number(3),
tutor_id number(3),
student_id number(4),
start_date date,
end_date date,
constraint pk_match_history primary key (match_id),
constraint fk1_match_history foreign key (tutor_id) references tutor(tutor_id),
constraint fk2_match_history foreign key (student_id) references student(student_id));

create table tutor_report
(match_id number(3),
month date,
hours number(3),
lessons number(3),
constraint pk_tutor_report primary key (match_id, month),
constraint fk1_tutor_report foreign key (match_id) references match_history(match_id));

insert into tutor values (100, '05-JAN-2017', 'Active');
insert into tutor values (101, '05-JAN-2017', 'Temp Stop');
insert into tutor values (102, '05-JAN-2017', 'Dropped');
insert into tutor values (103, '22-MAY-2017', 'Active');
insert into tutor values (104, '22-MAY-2017', 'Active');
insert into tutor values (105, '22-MAY-2017', 'Temp Stop');
insert into tutor values (106, '22-MAY-2017', 'Active');

insert into student values (3000, 2.3);
insert into student values (3001, 5.6);
insert into student values (3002, 1.3);
insert into student values (3003, 3.3);
insert into student values (3004, 2.7);
insert into student values (3005, 4.8);
insert into student values (3006, 7.8);
insert into student values (3007, 1.5);

insert into match_history values (1, 100, 3000, '10-JAN-2017', null);
insert into match_history values (2, 101, 3001, '15-JAN-2017', '15-MAY-2017');
insert into match_history values (3, 102, 3002, '10-FEB-2017', '01-MAR-2017');
insert into match_history values (4, 106, 3003, '28-MAY-2017', null);
insert into match_history values (5, 103, 3004, '01-JUN-2017', '15-JUN-2017');
insert into match_history values (6, 104, 3005, '01-JUN-2017', '28-JUN-2017');
insert into match_history values (7, 104, 3006, '01-JUN-2017', null);

insert into tutor_report values (1, '01-JUN-2017', 8, 4);
insert into tutor_report values (4, '01-JUN-2017', 8, 6);
insert into tutor_report values (5, '01-JUN-2017', 4, 4);
insert into tutor_report values (4, '01-JUL-2017', 10, 5);
insert into tutor_report values (1, '01-JUL-2017', 4, 2);

这是我到目前为止所拥有的:

Select (hours * 10) as amount paid from tutor_report group by month,  tutor_id

但是很明显,我不能只说最后的tutor_id。

2 个答案:

答案 0 :(得分:0)

如果要基于两个表上的列进行分组,则需要在匹配的ID上将它们连接起来,然后使用分组依据

Select (hours * 10) as amount paid 
from tutor_report a
 join match_history b on a. match_id = b.match_id 
group by month, tutor_id

答案 1 :(得分:0)

您可以加入match_history以获得tutor_id

但是您的语句和查询不匹配。如果要排序,请使用ORDER BY

SELECT tr.hours * 10 amount_paid
       FROM tutor_report tr
            INNER JOIN match_history mh
                       ON mh.match_id = tr.match_id
       ORDER BY tr.month,
                mh.tutor_id;

如果要聚合,则需要将hours作为某些聚合函数的参数。也许您要花费几个小时?

SELECT sum(tr.hours) * 10 amount_paid
       FROM tutor_report tr
            INNER JOIN match_history mh
                       ON mh.match_id = tr.match_id
       GROUP BY tr.month,
                mh.tutor_id;