Solved

Simple SQL Query required (or not so simple?)

Posted on 2013-11-05
9
163 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

688 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