Solved

Simple SQL Query required (or not so simple?)

Posted on 2013-11-05
9
161 Views
Last Modified: 2013-11-06
See simplified database attached.

See tblWhichMonth.  This very (very) simple table simply indicates which month we are currently processing.

See tblSalesByMonth which has a "sales" figure for each of the 12 months of the year.

I need a query that show my "Year To Date" sales figure.  That's it!

So for instance, if the tblWhichMonth.CurrentMonth = 4 then the total sales will be the sum of the first 4 "SalesValue" in the tblSalesByMonth
Database5.accdb
0
Comment
Question by:Patrick O'Dea
  • 5
  • 4
9 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39625828
Try this  - uses a SUM over records where the Month in the Sales table is less than or equal to the month in the WhichMonth table:

SELECT Sum(tblSalesByMonth.SalesValue) AS SumOfSalesValue
FROM tblSalesByMonth, tblWhichMonth
WHERE (((tblSalesByMonth.MonthNumber)<=[tblWhichMonth].[CurrentMonth]))

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 39625831
Simplified a little with aliases and removing the extra ()'s that access adds:

SELECT Sum(s.SalesValue) AS SumOfSalesValue
FROM tblSalesByMonth s, tblWhichMonth m
WHERE s.MonthNumber <= m.CurrentMonth

Open in new window

0
 

Author Comment

by:Patrick O'Dea
ID: 39625844
Thanks mbizup,

Great stuff so far but can I be allowed to complicate things slightly.

See attached.
Note that I have added a new field called "Salesman".

Can you give me some SQL that would give the the year to date sales (as before) and also group the sales by rep.

E.g.
Joe 48
Tim 53
Database5.accdb
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39625855
Just add on Salesman as a column, and include it in the group by clause:

SELECT s.SalesMan, Sum(s.SalesValue) AS SumOfSalesValue
FROM tblSalesByMonth s, tblWhichMonth m
WHERE s.MonthNumber <= m.CurrentMonth
GROUP BY s.SalesMan

Open in new window

0
 

Author Comment

by:Patrick O'Dea
ID: 39626770
Thanks again mbizup,

If it's okay can I tweak the question again.
If you prefer I can raise another question??

I have removed tblWhichMonth and replaced it with a more real life table called tblParameters.   The month number will now be determined by

tblParameters.ParameterValue where tblParameters.ParameterNumber =3

How do I include this in my "select" statement.
Database5.accdb
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39626773
Yeah - could you post another question please (this one is a bit different in structure)?  

Thanks!
0
 

Author Comment

by:Patrick O'Dea
ID: 39626819
New question on it's way!

By the way,  I can't help wonder what time zone you are in ..... I did not expect a response until much later in the day!
0
 

Author Closing Comment

by:Patrick O'Dea
ID: 39626864
First class answer.  Thanks!
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39626941
US East coast, but I'm usually up very early.  Its about 05:25 my time right now.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question