Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simple SQL Query required (or not so simple?)

Posted on 2013-11-05
9
Medium Priority
?
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 61

Accepted Solution

by:
mbizup earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

618 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