sch that includes student IDs and the periods for which the students are scheduled:
id period
111 1
111 2
111 3
111 4
111 5
att includes student IDs and the dates and periods they attended:
id date period
111 2009-10-01 1
111 2009-10-01 2
111 2009-10-01 3
111 2009-10-01 4
111 2009-10-01 5
111 2009-10-02 1
111 2009-10-02 2
111 2009-10-02 3
111 2009-10-02 5
111 2009-10-03 1
111 2009-10-03 2
111 2009-10-03 3
111 2009-10-03 5
id date period
111 2009-10-02 4
111 2009-10-03 4
left join on the tables between the IDs and periods but it's not getting me what I need. Any help is appreciated.
select distinct sch.*, date from sch, attsch table has ~900 rows and att has ~45,000. I think that's what's taking so long. Fortunately, I'll rarely need to run this.select a.id, a.[date], a.period from (select sch.id, dates.[date], sch.period from (select distinct [date] from att) dates, sch) a left join att on a.id = att.id and a.[date] = att.[date] and a.period = att.period where att.period is null
;WITH PerfectAttendance (StudentID, Period, [Date]) AS(
SELECT
sch.StudentID
, sch.Period
, att.[Date]
FROM sch
inner join att
on sch.StudentID=att.Studentid
group by
sch.studentid
, att.[Date]
,sch.period
)
SELECT
sch.StudentID
, sch.Period AS MissingPeriod
, sch.DayOfwork AS DayInQuestion
, att.[Date]
FROM PerfectAttendance sch
left join att
on sch.StudentID=att.Studentid and sch.[Date]=att.[Date] and sch.Period=att.Period
where att.[Date] IS NULL
SELECT
sch.StudentID
, sch.Period AS MissingPeriod
, sch.[Date] AS DayInQuestion
--, att.[Date]
FROM PerfectAttendance sch
left join att
on (sch.StudentID=att.Studentid and sch.[Date] =att.[Date] and sch.Period=att.Period)
where att.[Date] IS NULL
SELECT
pa.StudentID
, pa.Period AS MissingPeriod
, pa.[Date] AS DayInQuestion
FROM PerfectAttendance pa
EXCEPT
SELECT
att.StudentID
,att.Period
,att.[Date]
FROM
att
You are not logged in, either login or create an account to post comments
posted by mbrubeck at 12:36 PM on October 7 [1 favorite]