I working on a table that keeps record of student attending course. The data are keep using student identification number, course id, coursename, start date, end date. Student can take more than one course so there are repeating student identification number but with different course id, course name, start date and end date. What I’m trying to to here is to select students based on number of day using DATEDIFF.
Sorry for not explaining my problem properly. the structure of the table i’m talking about:
studcourse1(internalstudentid, staffnoic, courseid, coursenm, StDt, EndDt, location, organizer, generalcategorycd, generalsubcategorycd, eid)
staffnoic – staff identification number,
StDt – Start Date.
EndDt – End Date
I’ve checked, there’s no primary key or indexes on this table as it’s not a base but a view.
Sorry if the previous statement is too long. Let’s use this instead.
SELECT GradeGroupCd, StudCourse1.StaffNoIC, (DATEDIFF( EndDt, StDt ) +1) TotalDay, StDt, EndDt
FROM StudCourse1, tblStaff, tblRefTitleGred
WHERE tblStaff.TitleGredCd = tblRefTitleGred.TitleGredCd
AND StudCourse1.StaffNoIC = tblStaff.StaffNoIC
AND StDt >= '2009-1-1' AND YEAR(EndDt) <= YEAR(NOW())
AND (DATEDIFF( EndDt, StDt ) +1) > 90
AND (GeneralSubCategoryCd = 'S0012' OR GeneralSubCategoryCd = 'S0014')
GROUP BY GradeGroupCd, StudCourse1.StaffNoIC
The statement above fetches results for student(using staffnoic from table tblStaff and table StudCourse1) having taking course for more than 90 days using (DATEDIFF(EndDt, StDt) + 1). What that really confuse me is that for example a record from studcourse1 with staffnoic of ‘111111111111’, sample data:
studcourse1(internalstudentid, staffnoic, courseid, coursenm, StDt, EndDt, location, organizer, generalcategorycd, generalsubcategorycd, eid)
studcourse1(10629,111111111111,AAA1811,Course1,2010-01-01 00:00:00, 2010-12-31 00:00:00, '', ABC Org, G003, S0012, E00001812)
(30684,111111111111,AAA6968,Course2,2009-02-10 00:00:00, 2012-02-09 00:00:00, '', ABC Org, G003, S0012, E00006894)
(30685,111111111111,AAA6970,Course3,2011-01-01 00:00:00, 2012-02-09 00:00:00, '', ABC Org, G003, S0014, E00006896)
Running the SQL statement will select the one with the StDt of 2010-01-01 00:00:00 and EndDt of 2010-12-31 00:00:00. Why is it singling out that record and not others as they all fall under AND StDt >= ‘2009-1-1’ AND YEAR(EndDt) <= YEAR(NOW())”. Year now referring to year 2012. How can I make it select the one with the StDt(2009-02-09) and EndDt(2012-02-09)?
And also “(DATEDIFF( EndDt, StDt ) +1) > 90”, why does it add 1 to it? Wouldn’t DATEDIFF( EndDt, StDt ) > 90 be the right one?
Sorry if that too many questions. Just learn MySQL recently. Thank you for your time.
Ok it is hard to understand what you have written. Please provide the tables you have used and the structure.
Simplest method to get this done is having 3 tables,
1) students with student id, name and so on
2) Course – course id, course name and so on.
3) Enrolment – enroll id, start date, end date and foreign keys(student id and course id).
And I didnt get what you meant by (What I’m trying to to here is to select students based on number of day using DATEDIFF.)