473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Selecting distinct records with INNER JOIN

I built a student attendance tracking dB in version 2007 and am having trouble figuring out the perfect attendance portion. I want to return each student that does not have an unexcused or excused absence or has been remanded to alternative school. With the following code, I am getting more records than students so it seems there is an issue with the DISTINCT command as well as the WHERE clause. I can either select a quarter (nine weeks) from a drop down box, which automatically assigns dates to the from and to date fields, or I can manually put in dates.

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT Student.LastName, Student.FirstName, Student.MiddleName, Student.ClassPeriod, Attendance.AttendanceStatus FROM Student INNER JOIN Attendance ON Student.StudentID=Attendance.StudentID WHERE (((Attendance.AttendanceStatus)<>"ABSENT - EXCUSED" And (Attendance.AttendanceStatus)<>"ABSENT - UNEXCUSED" And (Attendance.AttendanceStatus)<>"REMANDED TO ALT. SCHOOL") And ((Attendance.AttendanceDate)>=forms!PerfectAttendance!FromDate.Value And (Attendance.AttendanceDate)<=forms!PerfectAttendance!ToDate.Value)) ORDER BY Student.ClassPeriod;
Jun 14 '14 #1
5 2843
Luuk
1,047 Expert 1GB
In your current selection, if a student has more than 1 'AttendanceStatus', he/she will show up more than once.

"I am getting more records than students so"
Are you getting duplicate students?

To check this you can expand the order by to:
Expand|Select|Wrap|Line Numbers
  1. ORDER BY Student.ClassPerdid, Student.LastName, Student.FirtName, Student.Middlename
or, if you have a studentnumber
Expand|Select|Wrap|Line Numbers
  1. ORDER BY Student.ClassPerdid, Student.StudentNumber
Jun 14 '14 #2
Thanks Luuk. I analyzed the data and found what was in common for the 4 duplicate students showing up. I have 68 students and 72 records being returned on report. They each had a tardy in their attendance records. Can I assume that since the tardies aren't in the WHERE clause, the DISTINCT is not being applied to records with tardies? I can actually live with duplicate records being returned if they have a tardy because they should be on perfect attendance report. Only absences or alternative school excludes a student.

I also deleted attendance records so that I only had nine students that had any absences and should not show up in the perfect attendance report. All 9 students are showing up in the perfect attendance report and I have verified that each has at least one absence. The date portion of the WHERE clause is working because if I select a quarter outside of any records, no records are returned. This is what I don't understand unless it has something to do with how the WHERE criteria is being grouped.

I tried something else as well. I took out the DISTINCT and it returned every attendance record except those that had an attendance status of an absence (no records have Alt. School) so it seems like the WHERE clause is working correctly. I'm thoroughly confused after doing this.

I'd truly appreciate any help. If you can think of an easier way to do this, I'm all for it. Even a count of absences would work so that I could quickly see anything above zero did not have perfect attendance. Problem is, I have no idea how to do that.
Jun 15 '14 #3
jimatqsi
1,271 Expert 1GB
Distinct means distinct. If it's not distinct it's a separate record. Look at the results returned by your query. Compare each row returned to every other row. You'll see what makes similar rows different from each other and then you'll understand why you have gotten multiple, distinct rows when you were expecting just one.

The solution may well be to return fewer columns or maybe it has to do with how you are grouping/totaling/sorting.

Jim
Jun 15 '14 #4
Thanks Jim. I remembered that I had a copy of the dB when I first built it and it only has limited attendance records in it. It is the same code I posted earlier that is not working. I even went in and changed a PRESENT status to ABSENT - EXCUSED or ABSENT - UNEXCUSED and it omits that student from the report like it should.
To keep testing, I went and added some attendance records to students in other periods. Worked the first time but when I went in and changed a present to ABSENT - UNEXCUSED or ABSENT - EXCUSED, it showed that student as having perfect attendance. I even closed out ACCESS and went back in but no luck. This makes me think it is ACCESS rather than the code if it works some of the time. Any ideas??

Thanks
Jun 15 '14 #5
Sorry Jim. I had a completely wrong understanding of the DISTINCT command. As soon as I added an attendance record with a status different from other records it shows that record. That makes it no longer distinct. I get it now. I'll see if I can fix with that in mind.
Jun 15 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ike | last post by:
Oh I have a nasty query which runs incredibly slowly. I am running MySQL 4.0.20-standard. Thus, in trying to expedite the query, I am trying to set indexes in my tables. My query requires four...
11
by: news-east.earthlink.net | last post by:
The scenario: two tables CustomerTable --------------- CustomerID OrderID CustomerName CustomerEmail
2
by: Matik | last post by:
Hello everyone, Small and (I think) very simple quesiton;-) which makes me creazy. Let's say I have two tables listed below: T1 ==== IDX ==== 1
0
by: Chris Kettenbach | last post by:
Good Morning, I have an xml file that's generated from a database. How do I select distinct values from a field in xslt and then loop through the records and produce output. Example ...
1
by: Edward | last post by:
I have two tables recording the sales transactions with purchase_id the common key Table 1 (Recording total amounts and customer info) Purchase_Id (Primary Key) Sales_Total (Total sales...
1
by: . | last post by:
My query works OK this way, but I need every "kohde_name" only once (preferred the earliest "hav_date"). I've tried with "DISTINCT kohde_name" in many ways, but always end up with syntax...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
18
by: Dave | last post by:
Guys I am really stuck on this one. Any help or suggestions would be appreciated. We have a large table which seemed to just hit some kind of threshold. They query is somewhat responsive when...
1
by: wasim12378 | last post by:
Hello out there here i am having difficulty in fetching the Brands Per ctegory in the sidebar of my website Here is the description .. I am using the feeds and feed uploader to upload feeds and...
3
by: WilliamWales | last post by:
Hi I have a mystery that I need some help. Table: Order_Header Fields:Fields:Order_number(PK),Client_Account_Number,Order_Date Table: Order_Detail (PK Order_number+Line_number)
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.