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 - SELECT [qry-yr-10-end-of-year-exams].[StudentID],
-
Sum([qry-yr-10-end-of-year-exams].[Weighting_applied]) AS SumOfWeighting_applied,
-
[qry-yr-10-end-of-year-exams].[Name]
-
FROM [qry-yr-10-end-of-year-exams]
-
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: - SELECT [qry-yr10-end-of-year-results_for_New_GCSE].StudentID,
-
[qry-yr10-end-of-year-results_for_New_GCSE].Name,
-
-
[qry-yr10-end-of-year-results_for_New_GCSE].SumOfWeighting_applied,
-
-
[%-2-GCSE-Grade].[GCSE-Grade]
-
-
FROM [qry-yr10-end-of-year-results_for_New_GCSE], [%-2-GCSE-Grade]
-
-
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 -
low , high , Grade
-
0, 19, U
-
20, 29, G
-
30, 39, F
-
40, 49, E
-
50, 59, D
-
60, 69, C
-
70, 79, B
-
80, 89, A
-
90, 100, A*
The two records missing are the ones where SumOfWeighting_applied = 69
and SumOfWeighting_applied = 79
Any ideas???
13 2552
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: - 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
The way to do this using a JOIN is: - SELECT [qry-yr10-end-of-year-results_for_New_GCSE].StudentID,
-
[qry-yr10-end-of-year-results_for_New_GCSE].Name,
-
[qry-yr10-end-of-year-results_for_New_GCSE].SumOfWeighting_applied,
-
[%-2-GCSE-Grade].[GCSE-Grade]
-
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
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
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()).
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.
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
@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
NeoPa 32,556
Expert Mod 16PB
A more reliable way to do it would be something like : - SELECT tR.StudentID
-
,tR.Name
-
,tR.SumOfWeighting_applied
-
,(
-
SELECT [GCSE-Grade]
-
FROM [%-2-GCSE-Grade]
-
WHERE [Low-perc]=(
-
SELECT Min([Low-Perc])
-
FROM [%-2-GCSE-Grade]
-
WHERE [Low-Perc]<=tR.SumOfWeighting_applied
-
)
-
)
-
-
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 >=.
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 :)
@NeoPa
Another piece of elegant SQL from you! But you do mean ">=" in the subquery WHERE clause, yes?
Pat
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)).
@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.
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 : - SELECT tR.StudentID
-
,tR.Name
-
,tR.SumOfWeighting_applied
-
,(
-
SELECT [GCSE-Grade]
-
FROM [%-2-GCSE-Grade]
-
WHERE [Low-perc]=(
-
SELECT Max([Low-Perc])
-
FROM [%-2-GCSE-Grade]
-
WHERE [Low-Perc]<=tR.SumOfWeighting_applied
-
)
-
)
-
-
FROM [qry-yr10-end-of-year-results_for_New_GCSE] AS [tR]
Well done for sticking to it :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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....
|
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...
|
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")
...
|
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...
|
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...
|
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.
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |