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

Simple Query for a Range (Not date related)

Hi Everyone,

This is my first topic so I hope you can help. I have searched but
can't seem to find what I am looking for, so here it is.

I want to create a query using design view in MS-Access that will show
the award amount for each persons sales each month based on the range
shown between High and Low Points.

Any help would be greatly appreciated! Thanks

Here are the columes I am dealing with

Sales Table :

ID Code Participant August September October
1 12526 Joe Smith 471 387 525
2 12556 Mary Lou 363 425 570
3 12586 Dan Leblanc 391 357 539
4 12544 Marc Breau 448 375 459
5 12698 Lucy White 354 400 490
Range Table

ID Low Points High Points Award
2 600 2000 $250.00
3 550 599 $225.00
4 500 549 $200.00
5 450 499 $175.00
6 400 449 $150.00
7 350 399 $125.00
8 300 349 $100.00
9 250 299 $75.00
10 200 249 $50.00
11 1 199 $25.00

Nov 20 '06 #1
4 1868
Tess2819 wrote:
Hi Everyone,

This is my first topic so I hope you can help. I have searched but
can't seem to find what I am looking for, so here it is.

I want to create a query using design view in MS-Access that will show
the award amount for each persons sales each month based on the range
shown between High and Low Points.

Any help would be greatly appreciated! Thanks

Here are the columes I am dealing with

Sales Table :

ID Code Participant August September October
1 12526 Joe Smith 471 387 525
2 12556 Mary Lou 363 425 570
3 12586 Dan Leblanc 391 357 539
4 12544 Marc Breau 448 375 459
5 12698 Lucy White 354 400 490
Range Table

ID Low Points High Points Award
2 600 2000 $250.00
3 550 599 $225.00
4 500 549 $200.00
5 450 499 $175.00
6 400 449 $150.00
7 350 399 $125.00
8 300 349 $100.00
9 250 299 $75.00
10 200 249 $50.00
11 1 199 $25.00
Using the data from your example:

qryAugustAward:
SELECT ID, Participant, [August], (SELECT Max(A.Award) FROM [Range] AS
A WHERE A.[Low Points] <= Sales.[August]) AS AugustAward FROM Sales;

!qryAugustAward:
ID Participant August AugustAward
1 Joe Smith 471 $175.00
2 Mary Lou 363 $125.00
3 Dan Leblanc 391 $125.00
4 Marc Breau 448 $150.00
5 Lucy White 354 $125.00

then insert:

, [September], (SELECT Max(A.Award) FROM [Range] AS A WHERE A.[Low
Points] <= Sales.[September]) AS SeptemberAward

and

, [October], (SELECT Max(A.Award) FROM [Range] AS A WHERE A.[Low
Points] <= Sales.[October]) AS OctoberAward

before ' FROM Sales' and change the name of the query :-). You really
don't want to hard code the SQL for each month like I've shown.
Perhaps another table can be used to narrow which months are selected
(with the year included) in the original crosstab used to obtain the
Sales table so that you know in advance which months will show up. My
intuition tells me that there's an elegant solution to this problem.

James A. Fortune
CD********@FortuneJames.com

Nov 20 '06 #2
Looks like you have some normalization issues with this setup. You
shouldn't be keeping a field in Sales Table for every month of the
year. You would be better off with a table setup like this:

EmployeeTable:
EmpID (pk), Code, Name

SaleTable
EmpID, Year, Month, SaleTotal

The SaleTable should use a composite key of EmpID-Year-Month to
uniquely identify seperate records.

Your Range Table is fine except that the HighPoints value is superflous
and not necessary under this structure.

For the structure you currently have, this query will return the
appropriate bonus based on August sales data:

SELECT SaleTable.Participant, SaleTable.August,
DLookUp("Award","RangeTable","LowPoints<=" & [August]) AS Bonus
FROM SaleTable LEFT JOIN RangeTable ON SaleTable.August =
RangeTable.LowPoints;

Again, while this does the job, you should closely consider changing
your current data structure.

Nov 21 '06 #3
Jamey Shuemaker wrote:
Looks like you have some normalization issues with this setup. You
shouldn't be keeping a field in Sales Table for every month of the
year. You would be better off with a table setup like this:

EmployeeTable:
EmpID (pk), Code, Name

SaleTable
EmpID, Year, Month, SaleTotal

The SaleTable should use a composite key of EmpID-Year-Month to
uniquely identify seperate records.

Your Range Table is fine except that the HighPoints value is superflous
and not necessary under this structure.

For the structure you currently have, this query will return the
appropriate bonus based on August sales data:

SELECT SaleTable.Participant, SaleTable.August,
DLookUp("Award","RangeTable","LowPoints<=" & [August]) AS Bonus
FROM SaleTable LEFT JOIN RangeTable ON SaleTable.August =
RangeTable.LowPoints;

Again, while this does the job, you should closely consider changing
your current data structure.
I see something elegant and something risky in your solution.
>From A97 Help (DLookup Function):
If more than one field meets criteria, the DLookup function returns the
first occurrence.
The elegant part is that it takes advantage of this behavior.

The risky part is that taking advantage of this behavior makes a risky
assumption.

In deciding the first occurrence, you're aided by the fact that ID
values exist. When ID values exist, Access usually uses them as a
default ordering for the table. Because DLookup does not specify how
the ordering is done, you're relying on that default ordering behavior.

Also from A97 Help (DLookup Function):

Whether you use the DLookup function in a macro or module, a query
expression, or a calculated control, you must construct the criteria
argument carefully to ensure that it will be evaluated correctly.
Maybe DMax would be safer?

Also, perhaps the OP can tell us whether the Sales Table is created by
a crosstab query or is created directly. If it is created directly I
agree with your normalization comments.

James A. Fortune
CD********@FortuneJames.com

Nov 21 '06 #4

CD********@FortuneJames.com wrote:
Jamey Shuemaker wrote:

I see something elegant and something risky in your solution.
From A97 Help (DLookup Function):

If more than one field meets criteria, the DLookup function returns the
first occurrence.
The elegant part is that it takes advantage of this behavior.

The risky part is that taking advantage of this behavior makes a risky
assumption.

Very true. I meant to comment on the reliance upon order, above, but
simply forgot. This will be particularly problematic if the balance
structure changes....say, we add a range like:

ID Low Award
12 2001 $300

Seems the balance structure has already changed once, since our ID=1
value has already dropped out of the table.

Probably better, in the long run, to normalize the tables, as above,
then setup a more rigorous query, like you mentioned in your first
response (obviously, altered for the new, normalized, table structure).

Nov 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: bcaponet | last post by:
I have a form where a user enters a date that I will then base a query on. In the past, I have simply placed Forms!! into the criteria for a query and it runs as long as the form is open. The...
3
by: Don Sealer | last post by:
I'm guessing this is pretty simple however not simple enough for me. I'm developing a database to track expenses, income, banking transactions, etc. I have a very simple query with four fields,...
7
by: Scott Frankel | last post by:
Still too new to SQL to have run across this yet ... How does one return the latest row from a table, given multiple entries of varying data? i.e.: given a table that looks like this: color...
5
by: jim Bob | last post by:
Hi, Can someone help with a simple query? I have a table with the following. Firstname Lastname InterviewerID1 InterviewerID2 InterviewerID3 ...
7
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I...
3
by: prabhas | last post by:
I want to swap two tuples in a table, using a single , simple query. No SELECT query allowed, no inner queries allowed. No PL/SQL allowed. Do you have any idea how to do this? e.g. my current...
2
by: jennwilson | last post by:
I am trying to generate a report based on a query that will list any records where an individual has a date listed that matches the specified time for one or both of the date fields. The two fields...
0
debasisdas
by: debasisdas | last post by:
This article consistes of some of the frequently asked date related queries. Hope the users find it useful. ========================== 1.Determining the Date of the First and Last Occurrence of a...
7
by: colintis | last post by:
I'm fixing a query that takes date filter on specific date (e.g. 10/13/2010). The date field in the table source also contains time along with the date. (E.g. 13/10/2010 6:26:45 AM) On the where...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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:
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...

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.