操作篇-判断明天哪些学员在校就餐(即交了餐费)

2013年07月21日 08:10
点击率:8813

分析如下:

首先要计算出明天上课学员信息
然后才能判断是否应当在校吃饭

首先我们来看一下判断某段日期内上课学员的代码:

Select distinct
StuClass.StudentID,
StuClass.StudentName
from LessonDegree,StuClass
where
LessonDegree.ClassID = StuClass.ClassID
and StuClass.ScStateID in (1,2)
and StuClass.Lessons > StuClass.CourseProgress
and LessonDegree.DateAndTime >= {@StartDate:开始日期}
and LessonDegree.DateAndTime >= {@EndDate:结束日期}

这段代码通过分析某段时间内的排课信息,关联学员选班信息,然后过滤重复数据,来实现某段时间内上课学员的推测。其中StuClass.ScStateID in (1,2)表示学员的选班状态一定要处于正常状态,StuClass.Lessons > StuClass.CourseProgress表示学员所购买的课程并没有上完,distinct过滤重复的数据。

查询某学员在某时间内是否交过某费用代码:

Select
count(*)
from BillItem,Bill
where
Bill.BillID = BillItem.BillID
and BillItem.ProductType = 2
and BillItem.ProductID = 18
and Bill.DateAndTime > #2012-6-1#
and Bill.DateAndTime < #2012-9-1#
and Bill.StudentID = 201300001

BillItem.ProductID 指明需要查询的收费项编号,Bill.DateAndTime > #2012-6-1#,Bill.DateAndTime > #2012-9-1#控制交费的时间范围,这是ACCESS语法,MS SQL需要将#号替换成’号即可。Bill.StudentID = 201300001表示查询的学员学号。

我们将两段代码进行整合:

Select
StudentID as 学号,
StudentName as 姓名

from

(
Select distinct
StuClass.StudentID,
StuClass.StudentName,

(
Select
count(*)
from BillItem,Bill
where
Bill.BillID = BillItem.BillID
and BillItem.ProductType = 2
and BillItem.ProductID = 18
and Bill.DateAndTime > #2012-6-1#
and Bill.DateAndTime < #2012-9-1#
and Bill.StudentID = StuClass.StudentID
) as Num

from LessonDegree,StuClass
where
LessonDegree.ClassID = StuClass.ClassID
and StuClass.ScStateID in (1,2)
and StuClass.Lessons > StuClass.CourseProgress
and LessonDegree.DateAndTime >= {@StartDate:开始日期}
and LessonDegree.DateAndTime >= {@EndDate:结束日期}

) as tb
where Num > 0

首先我们将“查询某学员在某时间内是否交过某费用”的代码做为一个子查询加入到上课学员信息中,并且以一个虚拟列“Num”存在。然后我们再将整个查询虚拟成一张表名称为“tb”,最后查询tb表,条件是Num大于0(表示至少交了一次午餐费),这样我们就能够精准的分析出明天需要在学校就餐的学员信息,为食堂人员提供了方便。


(把以上代码粘贴到《麦田培训学校管理软件》查询管理里,可直接使用)