473,397 Members | 2,068 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,397 software developers and data experts.

Comparison-query does not return all records

Dear all!
I have a database that stores student results - no, it's not a chart-graph question :):):) via the back door.

Within this database, sits a query called "qry-yr10-end-of-year-results". This query contains the fields "student_ID", "Name" and "SumofWeighting_applied".

This "sumofWeighting_applied" is nothing other than a calculated numerical value, I won't bore you with the resaons for its name.

The query returns 11 records, as it should do, and the SQL for the query is
Expand|Select|Wrap|Line Numbers
  1. SELECT [qry-yr-10-end-of-year-exams].[StudentID], 
  2. Sum([qry-yr-10-end-of-year-exams].[Weighting_applied]) AS SumOfWeighting_applied, 
  3. [qry-yr-10-end-of-year-exams].[Name]
  4. FROM [qry-yr-10-end-of-year-exams]
  5. GROUP BY [qry-yr-10-end-of-year-exams].[StudentID], [qry-yr-10-end-of-year-exams].[Name]
So far everything works as it should, the query returns 11 records, one for each of the students.

I would now like to link the numerical value stored in the field "SumOfWeighting_applied", to a Grade, a letter, in other words an 'A', a 'B', a 'C' etc.

For this, I have set up a table named "%-2-GCSE-Grade", containing the fields "low-Perc"; "high-Perc" and "GCSE-Grade". The idea comes from the O'Reilly Access Cookbook

Next I have created a qry called "qry_percentage_into_Grades"; this query contains the newly created table "%-2-GCSE-GRade" and the fully functioning query "qry-yr10-end-of-year-results"; by 'fully functioning, I mean, returning all 11 records.

I then specified the criteria for the field "SumOfWeighting_applied", using the statement

Between [%-2-GCSE-Grade].[low-perc] And [%-2-GCSE-Grade].[high-perc]

The complete sql for "qry_percentage_into_Grades" is:
Expand|Select|Wrap|Line Numbers
  1. SELECT [qry-yr10-end-of-year-results_for_New_GCSE].StudentID,
  2. [qry-yr10-end-of-year-results_for_New_GCSE].Name, 
  3.  
  4. [qry-yr10-end-of-year-results_for_New_GCSE].SumOfWeighting_applied, 
  5.  
  6. [%-2-GCSE-Grade].[GCSE-Grade]
  7.  
  8. FROM [qry-yr10-end-of-year-results_for_New_GCSE], [%-2-GCSE-Grade]
  9.  
  10. WHERE ((([qry-yr10-end-of-year-results_for_New_GCSE].SumOfWeighting_applied) Between [%-2-GCSE-Grade].[low-perc] And [%-2-GCSE-Grade].[high-perc]));
But for some reason that I cannot figure out, this query returns only 9 of the 11 records;

I double checked my table "%-2-GCSE-Grade"to make sure that I really do have a letter-Grade for each value but I think I have.


tbl %-2-GCSE-Grade
Expand|Select|Wrap|Line Numbers
  1. low ,   high ,  Grade
  2.  0,      19,      U
  3. 20,      29,      G
  4. 30,      39,      F
  5. 40,      49,      E
  6. 50,      59,      D
  7. 60,      69,      C
  8. 70,      79,      B
  9. 80,      89,      A
  10. 90,     100,      A*
The two records missing are the ones where
SumOfWeighting_applied = 69
and
SumOfWeighting_applied = 79

Any ideas???
Jun 26 '10 #1
13 2552
patjones
931 Expert 512MB
Hi,

Different database environments treat the BETWEEN operator differently. Some of them use it in an inclusive manner, meaning that if you say WHERE a BETWEEN b AND c - it will actually include the values b and c. Others will return only values that lie between b and c, and will actually leave b and c out. I think it is very telling that it is not returning the records with 69 and 79, as those are end values.

Try using this for your WHERE clause instead:

Expand|Select|Wrap|Line Numbers
  1. WHERE ((([qry-yr10-end-of-year-results_for_New_GCSE].SumOfWeighting_applied) >= [%-2-GCSE-Grade].[low-perc] AND ([qry-yr10-end-of-year-results_for_New_GCSE].SumOfWeighting_applied) <= [%-2-GCSE-Grade].[high-perc]))

There is also a way to do this using a JOIN operation, which is basically equivalent to a WHERE but a little cleaner. Let me know how it works out first.

Pat
Jun 27 '10 #2
patjones
931 Expert 512MB
The way to do this using a JOIN is:

Expand|Select|Wrap|Line Numbers
  1. SELECT [qry-yr10-end-of-year-results_for_New_GCSE].StudentID,
  2.        [qry-yr10-end-of-year-results_for_New_GCSE].Name,
  3.        [qry-yr10-end-of-year-results_for_New_GCSE].SumOfWeighting_applied,
  4.        [%-2-GCSE-Grade].[GCSE-Grade]
  5. FROM [qry-yr10-end-of-year-results_for_New_GCSE] INNER JOIN [%-2-GCSE-Grade] ON ([qry-yr10-end-of-year-results_for_New_GCSE].SumOfWeighting_applied >= [%-2-GCSE-Grade].[low-perc] AND [qry-yr10-end-of-year-results_for_New_GCSE].SumOfWeighting_applied <= [%-2-GCSE-Grade].[high-perc])

Like I said, this should give results equivalent to using a WHERE clause, but this method is a little easier for the SQL engine to optimize.

Pat
Jun 27 '10 #3
Thanks so much Pat
the thought had occured to me too but i dismissed it as soon as i realised that the query had picked up the value 49. this being the case, i just couldn't see why it didn't pick up the values 69 and 79. in the end, running out of time - it is world-cup-year after all - i changed the values in tbl_%_2_grades but your idea is lots neater, mine just happens to work this time round because no student happens to have the round values which now appear twice:
low-perc; high-perc; grade
50; 60; c
60; 70; b
70; 80; a

i know i'll have to change it soon.
Thanks again for the tip
all the best
Jun 28 '10 #4
NeoPa
32,556 Expert Mod 16PB
The values in the table are not all-inclusive I'm afraid.

They may show as integers, but the calculation of weighting surely result in fractional amounts and your table doesn't include values between 69.0 and 70.0 (nor for any other of the gaps).

For now I would make the upper and lower limits real numbers (EG. 69.999) rather than integers (or Longs) but if I can I'll look into knocking up some more reliable SQL to allow finding the correct value with just upper limits (using Max()).
Jun 28 '10 #5
NeoPa
32,556 Expert Mod 16PB
I should say that Between works on the inclusive criteria so making 20-30 impractical. Of course saying A>=20 AND A<30 would do the trick. There's still a purer way, but I'm working again now (lunch over) so unable to post it just yet.
Jun 28 '10 #6
NeoPa, you're a life saver :) which is not to say that I'm not grateful to all of you who've contributed.
But when under pressure to produce, a lay-man like myself tends to get a little nervous, so after trying Pat's Where clause with no success - probably 'cause I forgot a bracket somewhere, plugging the gaps by changing the perc-high field property to 3 fixed decimal points picked up the 2 remaining records.
So now I know that as far as my database is concerned,
79 is not always equal to 79
Thanks again
Jun 28 '10 #7
patjones
931 Expert 512MB
@NeoPa
Wow, I completely missed that aspect of the situation. So maybe what cheyenne thinks are values of 69 and 79 really aren't?

I used to run into something similar to this in FORTRAN all the time. You'd think you were looking at 43.00, and it was actually 43.00005 or something like that.

Pat
Jun 28 '10 #8
NeoPa
32,556 Expert Mod 16PB
A more reliable way to do it would be something like :
Expand|Select|Wrap|Line Numbers
  1. SELECT tR.StudentID
  2.       ,tR.Name
  3.       ,tR.SumOfWeighting_applied
  4.       ,(
  5.     SELECT [GCSE-Grade]
  6.     FROM   [%-2-GCSE-Grade]
  7.     WHERE  [Low-perc]=(
  8.         SELECT Min([Low-Perc])
  9.         FROM   [%-2-GCSE-Grade]
  10.         WHERE  [Low-Perc]<=tR.SumOfWeighting_applied
  11.         )
  12.     )
  13.  
  14. FROM   [qry-yr10-end-of-year-results_for_New_GCSE] AS [tR]
This works simply on the understanding that the low value is always whole, and needs no high value. This is an example that works with your original table layout. Personally, I'd change it round to use the high value instead and use Max() and < as opposed to the current Min() and >=.
Jun 28 '10 #9
NeoPa
32,556 Expert Mod 16PB
Pat,

I don't know if the values are all supposed to be integers and just slightly off due to being stored in variables of non-integral type, or calculated values that can fall anywhere in the range. We don't have the info to draw a conclusion on that. However, I'd infer, from the name of the field ([SumOfWeighting_applied]), that it may be the latter. The fact that both missing values ended in 9s was the clue that they fell ouside the range supplied. It really is better to define the range as a single comparison generally. Each record has it's own value and determining which a record matches is done by checking the value as valid and finding the closest match (as per my latest SQL).

All good fun :)
Jun 28 '10 #10
patjones
931 Expert 512MB
@NeoPa
Another piece of elegant SQL from you! But you do mean ">=" in the subquery WHERE clause, yes?

Pat
Jun 28 '10 #11
NeoPa
32,556 Expert Mod 16PB
Not in this case Pat.

As I am comparing the table values on the left with the student's achieved value on the right it needs to be <= here. Does that make sense?

My subsequent comments (Post #9) may have been somewhat confusing as they work from a different basis (They are talking about comparing the student score (left) against the table (right)).
Jun 28 '10 #12
patjones
931 Expert 512MB
@NeoPa
Right, I see. Maybe the Min() needs to be a Max() then? Something isn't clicking for me. Won't the innermost subquery always pick zero? I could be having a really bad day.
Jun 28 '10 #13
NeoPa
32,556 Expert Mod 16PB
You're absolutely right Pat (It should have been Max()).
The innermost subquery selects all records in the [%-2-GCSE-Grade] table where [Low-Perc] is less than or equal to the grade attained by the student. The value we are intereset in is actually the max of these, so Max() should be used in this case.

Here is a fixed version :
Expand|Select|Wrap|Line Numbers
  1. SELECT tR.StudentID
  2.       ,tR.Name
  3.       ,tR.SumOfWeighting_applied
  4.       ,(
  5.     SELECT [GCSE-Grade]
  6.     FROM   [%-2-GCSE-Grade]
  7.     WHERE  [Low-perc]=(
  8.         SELECT Max([Low-Perc])
  9.         FROM   [%-2-GCSE-Grade]
  10.         WHERE  [Low-Perc]<=tR.SumOfWeighting_applied
  11.         )
  12.     )
  13.  
  14. FROM   [qry-yr10-end-of-year-results_for_New_GCSE] AS [tR]
Well done for sticking to it :)
Jun 29 '10 #14

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

Similar topics

6
by: sam | last post by:
Hi, I got the the following syntax error in comparison: File "/usr/local/work/myparser.py", line 85 if ( (m=self.macro_parser.match (d)) != None ): ^ SyntaxError: invalid syntax How can I...
10
by: chandra.somesh | last post by:
Hi I recently had to write a small code in a competition ,but my code was rejected cause it failed in 1 of test cases. The problm was .....we are given vector of strings....each string...
29
by: Steven D'Aprano | last post by:
Playing around with comparisons of functions (don't ask), I discovered an interesting bit of unintuitive behaviour: >>> (lambda y: y) < (lambda y: y) False Do the comparison again and things...
3
by: baumann | last post by:
hi all, i could not understand the "unnecessary" pointer comparison. /* 207 * min()/max() macros that also do 208 * strict type-checking.. See the 209 * "unnecessary" pointer comparison....
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
5
by: MaSTeR | last post by:
Can anyone provide a practical short example of why in C# I shouldn't compare two strings with == ? If I write this in JAVA String string1 = "Widget"; if (string1 == "Widget") ...
4
by: Peter Kirk | last post by:
Hi I am looking at some code which in many places performs string comparison using == instead of Equals. Am I right in assuming that this will in fact work "as expected" when it is strings...
6
by: Tom Kelleher | last post by:
Folks, I have a client who will likely be looking at ASP.NET in the near future. They have an internal developer who is convinced there are "scalability problems" with ASP.NET. This client has...
7
by: bcutting | last post by:
I am looking for a way to take a large number of images and find matches among them. These images may not be exact replicas. Images may have been resized, cropped, faded, color corrected, etc. ...
37
by: Michele Simionato | last post by:
At work we are shopping for a Web framework, so I have been looking at the available options on the current market. In particular I have looked at Paste and Pylons and I have written my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.