从Oracle SQL中的每个日期计算年份

问题描述 投票:0回答:1

[菜鸟问题。我正在编写脚本来执行显示以下内容的报告:学生的姓氏和名字(以逗号分隔),注册年份,学术顾问的姓氏和名字(以逗号分隔)排序:入学年份过滤条件:仅包括当前活跃的学生

努力计算入学年数并筛选活跃学生。

我正在使用的表:enter image description here

SELECT CONCAT(CONCAT(Student.Last_Name, ', '), Student.First_Name) AS "Student", 

Student_Enrollment_Status.Date_Status_Updated,

CONCAT(CONCAT(Faculty.Last_Name, ', '), Faculty.First_Name) AS "Advisor"

FROM Student

WHERE Faculty.Faculty_ID = Student.Advisor_ID

AND Student(Student_ID) = Student_Enrollment_Status(Student_ID)

AND Student_Enrollment_Status(Status_ID) = Enrollment_Status (Status_ID);
sql oracle-sqldeveloper oracle-apex
1个回答
0
投票

将会是这样:

  • 第1-22行代表样本数据(您无需输入)
  • 您需要的查询从第24行开始
  • 注册年数是通过减去当前注册年和年份来计算的;那是相当不准确的,但是-您没有解释exactly的意思
  • 我认为活动状态是ID(2、3、5)

SQL> with
  2  student (student_id, first_name, last_name, advisor_id) as
  3    (select 1, 'ash', 'smith', 9 from dual union all
  4     select 2, 'tash', 'paul', 8 from dual union all
  5     select 3, 'carl', 'wall', 6 from dual union all
  6     select 4, 'fred', 'john', 3 from dual),
  7  student_enrollment_status (student_id, status_id, date_status_updated) as
  8    (select 1, 2, date '2017-09-04' from dual union all
  9     select 2, 3, date '2018-09-05' from dual union all
 10     select 3, 3, date '2018-09-05' from dual union all
 11     select 4, 2, date '2019-09-04' from dual),
 12  enrollment_status (status_id, status) as
 13    (select 2, 'enrolled' from dual union all
 14     select 3, 'on leave' from dual union all
 15     select 4, 'full time' from dual union all
 16     select 5, 'part time' from dual union all
 17     select 6, 'withdrawn' from dual),
 18  faculty (faculty_id, first_name, last_name) as
 19    (select 9, 'jane', 'gold' from dual union all
 20     select 8, 'sam', 'greene' from dual union all
 21     select 6, 'mark', 'west' from dual union all
 22     select 3, 'jen', 'dash' from dual)
 23  --
 24  select s.last_name ||', '|| s.first_name student,
 25    extract(year from sysdate) - extract(year from ses.date_status_updated)
 26      years_enrolled,
 27    f.last_name ||', '|| f.first_name advisor
 28  from student s join student_enrollment_status ses on ses.student_id = s.student_id
 29    join enrollment_status es on es.status_id = ses.status_id
 30    join faculty f on f.faculty_id = s.advisor_id
 31  where es.status_id in (2, 3, 5)
 32  order by years_enrolled;

STUDENT     YEARS_ENROLLED ADVISOR
----------- -------------- ------------
john, fred               1 dash, jen
paul, tash               2 greene, sam
wall, carl               2 west, mark
smith, ash               3 gold, jane

SQL>
© www.soinside.com 2019 - 2024. All rights reserved.