麻烦了解基本的sql语句

时间:2015-01-27 04:02:58

标签: sql database ms-access

我坚持使用sql语句并需要一些帮助。我是数据库和访问2007的新手,所以我不完全知道所有的语法,任何指导都会很好,谢谢

一些信息

Teacher = [CourseN, Quarter,TeacherName]
Course = [CourseN,CourseName, Nunit)
Section = [CourseN, Quarter , DayTime,  RoomN]/ Examples of DayTime: M2:00AM, 
W4:50PM, and T8:00PM. Note that DayTime is represented as a string.
Student = [studentName, CourseN, Quarter]

问题

13. List the CourseN and Quarter of every course taught by two 
different instructors in the same quarter ordered by the CourseN in descending order.

教师表

CourseN Quarter TeacherName
1   Winter 2011 Karen Reed
1   Winter 2011 Sun     <--- cant add this one which i need to make it   work
2   Spring 2005 Salloum
3   Spring 2005 Karen Reed
4   Spring 2005 Salloum
5   Winter 2011 Sun

关于第二个的问题是它不会让我把重复的课程N放在我的数据库表中因为它是索引,主键或关系所以我不知道该怎么做因为我无法在我的表中输入

编辑:我删除了自我解决后的第一个问题,所以帖子中没有太多内容

1 个答案:

答案 0 :(得分:2)

我认为你需要这样的东西。

对于第一个查询,您需要通过课程编号加入表“课程”和“部分”,这是关键。现在,我认为您不需要在dayTime上进行任何字符串操作,因为您只关心聚合将保留的记录数。

对于第二个查询,您需要按课程编号连接表“课程”和“教师”。

两个查询都应该使用Group By和Having子句,因为您需要聚合两者,这取决于教师/每周会议的数量。

希望这有帮助。

drop table #Teacher
SELECT  *
into #Teacher
FROM    
(
    select 1 as CourseN, 1 as Quarter, 'Dan' as TeacherName
        union
    select 1 as CourseN, 1 as Quarter, 'Alex' as TeacherName
        union
    select 2 as CourseN, 1 as Quarter, 'Rob' as TeacherName
        union
    select 3 as CourseN, 2 as Quarter, 'Jim' as TeacherName
        union
    select 4 as CourseN, 3 as Quarter, 'Bob' as TeacherName
        union
    select 4 as CourseN, 3 as Quarter, 'Alice' as TeacherName
) a

drop table #Course
SELECT  *
into #Course
FROM    
(
select 1 as CourseN, 'English' as CourseName, 1 as Nunit
    UNION
select 2 as CourseN, 'Algebra' as CourseName, 1 as Nunit
    UNION
select 3 as CourseN, 'Math' as CourseName, 1 as Nunit
    UNION
select 4 as CourseN, 'Science' as CourseName, 1 as Nunit
) a


drop table #Section
SELECT  *
into #Section
FROM    
(
    select 1 as CourseN, 1 as Quarter, 'M2:00AM' as DayTime , 1 as RoomN
        UNION
    select 1 as CourseN, 1 as Quarter, 'W2:00AM' as DayTime , 1 as RoomN
        UNION
    select 2 as CourseN, 1 as Quarter, 'W2:00AM' as DayTime , 1 as RoomN
        UNION
    select 3 as CourseN, 2 as Quarter, 'W2:00AM' as DayTime , 1 as RoomN
        UNION
    select 3 as CourseN, 2 as Quarter, 'T2:00AM' as DayTime , 1 as RoomN
        UNION
    select 4 as CourseN, 3 as Quarter, 'T2:00AM' as DayTime , 1 as RoomN
) a


--List the CourseN, CourseName, and Quarter which meets or met at least  two times a week.
select a.CourseN , a.CourseName , b.Quarter --, COUNT(b.CourseN) NumOfWeeklyMeetings
from #Course a
    inner join #Section b
        on a.CourseN = b.CourseN
Where 1=1
GROUP BY a.CourseN , a.CourseName , b.Quarter
having COUNT(b.CourseN) > 1


--List the CourseN and Quarter of every course taught by two 
--different instructors in the same quarter ordered by the CourseN in descending order.
SELECT  a.CourseN , b.Quarter --, count(b.TeacherName) NumOfTeachers
FROM    #Course a
    inner join #Teacher b 
        on a.CourseN = b.CourseN
WHERE 1=1
group by a.CourseN , b.Quarter
having count(b.TeacherName) > 1