473,387 Members | 1,779 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,387 software developers and data experts.

Comparing Results of Two Queries with User Input

I have an Access DB for our non-profit Children's Home (we give kids that are having problems a home and help them graduate from HS) that keeps track of contributors and contributions. I have created a query that asks user for date input (start and end date) and will give a list of all contributions and contributor information that were made between that date range.

However, I now need to be able to COMPARE this information for TWO different date ranges and put it into a report.

I can't seem to figure out how to do this. I've tried to make two separate queries and join them but it never asks for the 2nd date range in anything I've tried.

I need to get this information for a grant that we have applied for and they want to see contributor information from last year and part of this year for each contributor so that we can show which ones gave MORE this period and they will then match that number for the amount of the grant - so this is huge for us.

ANY information on this would be helpful. I don't even know where to start on this one. Thank you!
Jul 12 '15 #1

✓ answered by NeoPa

Database work relies on precision. This is particularly true of the specification or expression of the requirement.

I'm afraid your specification is too vague to do much with ATM. Can you be more precise and detailed? An explanation of how and where your data is stored is fundamental before any really helpful advice can be given. IE. that is specific and not too general.

I would guess that you want, for each contributor, to compare the total amount donated within the first date range with the that same total within the second date range. Possibly adding a value that indicates the difference.

Assuming a table of contributors called [tblContributor] and a table of contributions called [tblContribution] you might be looking at something like :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [tCr].[ContributorID]
  2.        , [tCr].[ContributorName]
  3.        , Sum(IIf([tCn].[ConDate]Between [Enter Start of First Period] And [Enter End of First Period],[tCn].[ConValue],0)) AS [TotFirst]
  4.        , Sum(IIf([tCn].[ConDate]Between [Enter Start of Second Period] And [Enter End of Second Period],[tCn].[ConValue],0)) AS [TotSecond]
  5.        , [TotSecond]-[TotFirst] AS [MoreThan]
  6. FROM     [tblContributor] AS [tCr]
  7.          INNER JOIN
  8.          [tblContribution] AS [tCn]
  9.   ON     [tCr].[ContributorID]=[tCn].[ContributorID]
  10. WHERE    ([tCn].[ConDate] Between [Enter Start of First Period] And [Enter End of First Period])
  11.    OR    ([tCn].[ConDate] Between [Enter Start of Second Period] And [Enter End of Second Period])
  12. GROUP BY [tCr].[ContributorID]
  13.        , [tCr].[ContributorName]
In this instance the query would run and the operator be prompted for the four date values that specify the two date ranges. This is not the only way to handle this but in the absence of any more information from you about the design it is an illustrative example at least.

7 1138
NeoPa
32,556 Expert Mod 16PB
Database work relies on precision. This is particularly true of the specification or expression of the requirement.

I'm afraid your specification is too vague to do much with ATM. Can you be more precise and detailed? An explanation of how and where your data is stored is fundamental before any really helpful advice can be given. IE. that is specific and not too general.

I would guess that you want, for each contributor, to compare the total amount donated within the first date range with the that same total within the second date range. Possibly adding a value that indicates the difference.

Assuming a table of contributors called [tblContributor] and a table of contributions called [tblContribution] you might be looking at something like :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [tCr].[ContributorID]
  2.        , [tCr].[ContributorName]
  3.        , Sum(IIf([tCn].[ConDate]Between [Enter Start of First Period] And [Enter End of First Period],[tCn].[ConValue],0)) AS [TotFirst]
  4.        , Sum(IIf([tCn].[ConDate]Between [Enter Start of Second Period] And [Enter End of Second Period],[tCn].[ConValue],0)) AS [TotSecond]
  5.        , [TotSecond]-[TotFirst] AS [MoreThan]
  6. FROM     [tblContributor] AS [tCr]
  7.          INNER JOIN
  8.          [tblContribution] AS [tCn]
  9.   ON     [tCr].[ContributorID]=[tCn].[ContributorID]
  10. WHERE    ([tCn].[ConDate] Between [Enter Start of First Period] And [Enter End of First Period])
  11.    OR    ([tCn].[ConDate] Between [Enter Start of Second Period] And [Enter End of Second Period])
  12. GROUP BY [tCr].[ContributorID]
  13.        , [tCr].[ContributorName]
In this instance the query would run and the operator be prompted for the four date values that specify the two date ranges. This is not the only way to handle this but in the absence of any more information from you about the design it is an illustrative example at least.
Jul 12 '15 #2
Thank you for your reply! Yes, you are correct that I want to compare, for each contributor the total amount donated within the first date range to the total amount donated in the second date range and with a value that indicates the difference. Ideally, it would only show the contributors that donated MORE in the second date range and filter out any that gave the same or less.

I think your code is a good start and I will try to input my own table (and other names) for the ones you have here (although they are VERY close!).

Information on my DB is:

tblContributor with the following:
ContributorID
LastName/OrganizationName
FirstName

and

tblReceipts with
ContibutorID
ContributionDate
Amount

I will try to put MY names into the code you posted and see how it works. Again, thank you so much! I will post back with my results.
Jul 12 '15 #3
Here is my code ... see below. When I run the query, it asks for the two sets of dates (start and end of first period, start and end of second period). However, it then asks for
  1. tblReceipts.Amount
  2. Enter End of First Period
  3. tblRecipts.ContributionDate
  4. Enter End of Second Period

I tried entering some data into those and ran the query and it gives me TotFirst with contribution amounts and TotSecond with 0 for contribution amounts. And it does the math and gives me results in the MoreThan column.

On your code, I wasn't sure what this part was doing:
Expand|Select|Wrap|Line Numbers
  1. FROM     [tblContributor] AS [tCr]
  2.          INNER JOIN
  3.          [tblContribution] AS [tCn]
  4.   ON     [tCr].[ContributorID]=[tCn].[ContributorID]
So I entered my table names as you will see below.

Here is my query:
Expand|Select|Wrap|Line Numbers
  1. SELECT   [tblContributor].[ContributorID]
  2.        , [tblContributor].[LastName/OrganizationName]
  3.        , Sum(IIf([tblReceipts].[ContributionDate]Between [Enter Start of First Period] And [Enter End of First Period],[tblReceipts].[Amount],0)) AS TotFirst
  4.        , Sum(IIf([tblReceipts].[ContributionDate]Between [Enter Start of Second Period] And [Enter End of Second Period],[tblRecipts].[Amount],0)) AS TotSecond
  5.        , [TotSecond]-[TotFirst] AS MoreThan
  6. FROM     tblContributor AS tblContributor
  7.          INNER JOIN
  8.          tblReceipts AS tblReceipts
  9.   ON     [tblContributor].[ContributorID]=[tblReceipts].[ContributorID]
  10. WHERE    ([tblReceipts].[ContributionDate] Between [Enter Start of First Period] And [Enter End of First Period])
  11.    OR    ([tblRecipts].[ConributionDate] Between [Enter Start of Second Period] And [Enter End of Second Period])
  12. GROUP BY [tblContributor].[ContributorID]
  13.        , [tblContributor].[LastName/OrganizationName];
Jul 12 '15 #4
NeoPa
32,556 Expert Mod 16PB
PLeming:
On your code, I wasn't sure what this part was doing:
The AS keyword in SQL is an alternative for ALIAS. That means that wherever we want to refer to the long table name [tblContributor] we can use the shorter version, [tCn], instead. This saves filling the overall SQL string with lots of copies of a value that gives the reader a lot of work to do in order to interpret what it's doing. SQL doesn't care, but humans can work more easily without all the extra data to process. It can be used to give alternative names to tables as well as to give usable names to calculated values such as [MoreThan].

Other than that, and from the information you've told us, you've actually made a perfect job of translating my example into your system. It would seem though, that the information you've shared isn't 100% accurate. Had it been, because I can tell that you've transcribed the logic perfectly, it would certainly not have complained about the references to [tblReceipts].[Amount] & [tblReceipts].[ContributionDate].

My first thought was that [tblReceipts] is actually [tblReceipt]. Consistent with the other table name. However, that would have caused all references to any of its fields to fail but apparently [tblReceipts].[ContributorID] didn't. Therefore I can only guess that the other two fields are misspelled in some way. Certainly this is the area to look at closely for the problems and certainly, also, being prompted for those values indicates a problem.
Jul 12 '15 #5
THANK YOU!!! You were right, I did have a couple of misspellings. I fixed those and now it works perfectly!

I do have a question, though. From the results that I get, I don't see anything for contributors who have 0 donations during the two ranges. Is that built in to the query? It will only retrieve data from contributors who had donations in one or both of the two ranges? If so, that is exactly what I needed as I didn't want a list of ALL contributors if they didn't make any donations.

Thank you so much for all your help!!!!
Jul 12 '15 #6
NeoPa
32,556 Expert Mod 16PB
PLeming:
From the results that I get, I don't see anything for contributors who have 0 donations during the two ranges. Is that built in to the query? It will only retrieve data from contributors who had donations in one or both of the two ranges? If so, that is exactly what I needed as I didn't want a list of ALL contributors if they didn't make any donations.
Yes. Indeed.

To change this so that all contributors are included you would need to change two things in the current SQL that ensure only those with contributions within either of the time frames are included.
  1. INNER JOIN would need to be changed to LEFT JOIN.
    The first means that data is only returned when records from both tables match each other. IE. A Contributor and a Receipt match up. The second will return all Contributor data and any Receipt data that does match. Where there is no match then all Receipt fields will be returned as Nulls.
    See SQL JOINs for more on that.
  2. The WHERE clause would need to be changed to ensure that Nulls (Remember how non-existent Receipt records are returned from point #1 above.) are also accepted as valid values for [tblReceipt].[ContributionDate].

NB. I switched the posts flagged as the Best Answer as it's mainly to help other people looking for solutions for similar questions.
Jul 12 '15 #7
Thank you for the additional information. And also for flagging the correct answer as "Best". I realized afterward that the post didn't contain the whole thread. :)
Jul 12 '15 #8

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

Similar topics

3
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This file is basically a list of regular expressions. What...
8
by: rgonzale6 | last post by:
Hello. I'm using SQL query analyzer to run some queries against an SQL2000 DB. Unfortunately, my previous SQL experience is some Oralce SQL I took in school. To put it simply, I'm trying to...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
2
by: danielboendergaard | last post by:
Hey Im making a homepage in php. I use a html form to put data into mysql and i want to make some buttons which inserts user input values into a textarea. I have used a button like this: <input...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
4
waynetheengineer
by: waynetheengineer | last post by:
Hi, I was wondering if anyone had any suggestions on my database: I have a form that accepts user input into a single text box. When the OK button is hit, a query should search for all records...
7
by: vaiism | last post by:
I am creating a report that outputs the contact information and details about a water treatment plant, and needs to include information about people who work there. If I tie all the information...
1
by: Barb.Richards | last post by:
I have created an append query that pulls information from one database, and will append the selected information into a new table. The fields are setup like 'number' 'category' 'code' 'shares' and...
3
by: rickytb | last post by:
Hey all, I've hit a snag on a beginner Java problem involving building occupancy. I'm supposed to get user input from a GUI. My main problem is that I'm totally lost on how to take the information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...

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.