难以理解基本的 sql 语句
trouble understand basic sql statements
我卡在 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.
老师Table
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
关于第二个的问题是它不允许我将重复的 courseN 放入我的数据库中 table 因为它是索引、主键或关系所以我不知道该怎么做,因为我无法输入它在我的 table
编辑: 我删除了第一个问题,因为我解决了,所以post
我想你需要这样的东西。
对于第一个查询,您需要按课程编号连接表 "Course" 和 "Section",这是关键。现在,我认为您不需要在 dayTime 上进行任何字符串操作,因为您只关心聚合将保存的记录数。
对于第二个查询,您将需要再次按课程编号连接表 "Course" 和 "Teacher"。
这两个查询都应该使用 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
我卡在 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.
老师Table
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
关于第二个的问题是它不允许我将重复的 courseN 放入我的数据库中 table 因为它是索引、主键或关系所以我不知道该怎么做,因为我无法输入它在我的 table
编辑: 我删除了第一个问题,因为我解决了,所以post
我想你需要这样的东西。
对于第一个查询,您需要按课程编号连接表 "Course" 和 "Section",这是关键。现在,我认为您不需要在 dayTime 上进行任何字符串操作,因为您只关心聚合将保存的记录数。
对于第二个查询,您将需要再次按课程编号连接表 "Course" 和 "Teacher"。
这两个查询都应该使用 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