Solved

Simple SQL Query required (or not so simple?)

Posted on 2013-11-05
9
158 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now