473,406 Members | 2,745 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,406 software developers and data experts.

How to Formulate this Access Query

Hi,

I have a access database table with sales data in it, detailing sales rep, customer, date of sale, sale item etc.

I need to generate a query which will list which customers haven't brought off us in the last week, month, 6 months (input variable) and the sales rep responsible for the store.
eg
customer name, rep, date of last sale/ most recent sale
store a, tom, 17/3/2011
store b, joe, 1/1/2011

Then i know joe needs to go to store b and make a sale because they haven't purchased anything from us in the last 3 months. My problem has been calculating the lastest sales date. In excel i would calculate the number of days since sale for each company then use the small function to the most recent sale.

Thanks

Alex
Mar 17 '11 #1
10 2440
NeoPa
32,556 Expert Mod 16PB
This seems pretty straightforward Alex. Assuming all the details are accurate.

What you're looking to find is a list of all records where the Date stored is less than the current date - a variable factor. The first step then is to convert the value entered for the period (1 week; 1 month; 3 months; etc) into a recognisable SQL date literal (See Literal DateTimes and Their Delimiters (#)). I suspect that using datPeriod = DateAdd(?, ?, Date()) would be good here. I don't have enough detail from you to specify the two missing parameters but I'm sure you can work them out.

Assuming then, that you now have a variable (datPeriod) set up with the value that reflects the start of the period you want to scan back over, you can create the following string variable (strSQL) to contain the SQL you need to use to list the relevant items from your table :
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * " & _
  2.          "FROM   [tblSalesOV] " & _
  3.          "WHERE  ([LastSaleDate] < " & _
  4.          Format(datPeriod, "\#m/d/yyyy\#") & ")"
Clearly, I've had to guess at some of your field names, but it should be clear what you need to change to get it to work on your database.
Mar 17 '11 #2
Hi NeoPa,

Thanks for your response.

Using your code would tells me all the sales made before a certain period of time. So company A may appear 50 times in that list. But don't care about 49 of them. I just need know the most recent sale to that company.

Bescially I need a list of every compnay i sell to, with the date of most recent sale to that company.

Thanks again

Alex
Mar 17 '11 #3
Mariostg
332 100+
Simply adapt NeoPa'a query by adding SELECT TOP 1 * and GROUP BY [LastSaleDate] DESC
Mar 17 '11 #4
NeoPa
32,556 Expert Mod 16PB
According to your post Alex, what data you had in which form wasn't exactly clear. I misread it as saying the data was already aggregated (hence my suggested code). If the data is as described in the first paragraph of your post then try instead something like :

Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT   [Customer]" & _
  2.          "       , [Sales Rep]" & _
  3.          "       , Max([SaleDate]) AS [LastSaleDate] " & _
  4.          "FROM     [tblSalesOV] " & _
  5.          "GROUP BY [Customer]" & _
  6.          "       , [Sales Rep]" & _
  7.          "HAVING   ([LastSaleDate] < " & _
  8.          Format(datPeriod, "\#m/d/yyyy\#") & ")"
Mar 17 '11 #5
Hi MarioStg,
Thanks for the reply

Your idea works well thanks. I am currently using the following SQl query

Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 1 SalesTable.ShopNumber, CustomerTable.[Shop name], CustomerTable.[Sales rep], SalesTable.SaleDate, Date()-[saledate] AS DaysAgo
  2. FROM CustomerTable INNER JOIN SalesTable ON CustomerTable.[Shop Number] = SalesTable.ShopNumber
  3. WHERE (((SalesTable.ShopNumber)=[Which shop number?]))
  4. ORDER BY SalesTable.ShopNumber, SalesTable.SaleDate DESC;
Now at the moment, it requests a input for the shop number and gives back the most recent sale. :)

I would now like to expand it. I would like use the query over and over again incrementing the shop number 1,2,3,4 etc and give one single report back. I guess this will require a VBA macro, and some method to collect all the result together.

Do you have any ideas?

Thanks in advance

Alex
Mar 17 '11 #6
Mariostg
332 100+
There are certainly more than one way to achieve this. But I cannot do it for you. I prefer to give hints when you are in trouble. Maybe someone will be willing to do it for you. I strongly recommend you start working on a solution. You might get better support that way.
Mar 17 '11 #7
NeoPa
32,556 Expert Mod 16PB
Using TOP 1 will enable you to find the max for a specific grouping, but unfortunately doesn't scale well. As in, it doesn't work for multiple groupings (except by repeating the query over and over again).

If you look again at post #5 (clearly I was still working on the revised version as you posted your last) you should find the basics of how to achieve what you need. As Mario says, we prefer to, and are actively encouraged to, provide help while not providing boiler-plate answers for you (And I'm sure you're not expecting any more). If you have any difficulty implementing the concepts there into your own, already developed, SQL then please let us know. I'm sure we can help.
Mar 17 '11 #8
Hi NeoPa and Mariostg,

Thanks for your help I will try working on it today. Yesteday was my first day using access. I had used WQL before, but not SQL. Thanks
Mar 18 '11 #9
thank you very much

Expand|Select|Wrap|Line Numbers
  1. SELECT CustomerTable.[Shop Number], CustomerTable.[Sales rep], Max(SalesTable.[SaleDate]) AS LastSaleDate
  2. FROM CustomerTable INNER JOIN SalesTable ON CustomerTable.[Shop Number] = SalesTable.ShopNumber
  3. GROUP BY CustomerTable.[Shop Number], CustomerTable.[Sales rep];
Has worked exactly as I needed it

Thanks Again

Alex
Mar 18 '11 #10
NeoPa
32,556 Expert Mod 16PB
Pleased to hear it Alex (and you're doing pretty well for a first-timer).

I notice there is nothing to exclude customers which fall outside of the range (The HAVING clause) so I expect that is either handled elsewhere or the data is convenient simply to scan through.
Mar 18 '11 #11

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

Similar topics

43
by: Woodies_46 | last post by:
Hi all, I'm just a little bit stuck... what i have is a form with tick boxs and text boxs and stuff like that on it and a search button. What I would like to be able to do is for the user to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.