Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
splat78423
Starting Member
3 Posts |
Posted - 2007-03-22 : 11:16:20
|
| So these are a couple different interpretations I've made of a statement that needs to return students name's with the OSOS field in the attendcode code column of the attendance table on todays date with an attendance.aperiod column equal to 4.This first one returns no results at all when I should get about 7 rows or so.....Select Student.name, attendance.studentid, attendance.attenddate, attendance.aperiod, attendance.attendcodefrom Attendance, Studentwhere AttendDate >= DateAdd(Day, DateDiff(Day, 0, GetDate()), 0) And AttendDate < DateAdd(Day, DateDiff(Day, 0, GetDate()), 0) + 1 And aperiod = '4' And AttendCode = 'OSOS' AND student.name = Attendance.studentidThis one is returning all of the names in the database 6 times each.Select Student.name, attendance.studentid, attendance.attenddate, attendance.aperiod, attendance.attendcodefrom Attendance, Studentwhere AttendDate >= DateAdd(Day, DateDiff(Day, 0, GetDate()), 0) And AttendDate < DateAdd(Day, DateDiff(Day, 0, GetDate()), 0) + 1 And aperiod = '4' And AttendCode = 'OSOS' order by nameI think something is messed up with my WHERE control but I cant figure it out... Any help is greatly appreciated! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-22 : 11:27:05
|
| Yes. You have to tie the two tables attendance and Student together!use either where attendance.studentid = students.studentidoruse inner join students on students.studentid = attendance.studentid -- this is to prefer.Peter LarssonHelsingborg, Sweden |
 |
|
|
splat78423
Starting Member
3 Posts |
Posted - 2007-03-22 : 11:34:30
|
| I joined them together in the first example but I got no results when tried that. |
 |
|
|
splat78423
Starting Member
3 Posts |
Posted - 2007-03-22 : 11:38:27
|
| I tried the following...Select Student.name, attendance.studentid, attendance.attenddate, attendance.aperiod, attendance.attendcodefrom Attendance, Studentwhere attendance.studentid = student.studentidAND AttendDate >= DateAdd(Day, DateDiff(Day, 0, GetDate()), 0) And AttendDate < DateAdd(Day, DateDiff(Day, 0, GetDate()), 0) + 1 And aperiod = '4' And AttendCode = 'OSOS' order by namethen got 6 results....it appears to have worked! So I guess you were right, I was simply not joining the two tables together correctly!Thanks so much for the help! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-22 : 11:45:37
|
See? I am a mind-reader... You were almost there in your first query. But you tied Student.Name with Attendance.StudentID instead.Select s.name, a.studentid, a.attenddate, a.aperiod, a.attendcodefrom Attendance AS ainner join Student AS s ON s.studentid = a.studentidWHERE a.AttendDate >= DateAdd(Day, DateDiff(Day, 0, current_tiemstamp), 0) And a.AttendDate < DateAdd(Day, DateDiff(Day, 0, current_timestamp), 1) And a.aperiod = '4' And a.AttendCode = 'OSOS' order by s.name Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|