473,473 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Need a Query to Run X times for a date range

4 New Member
Hi Everyone, I'm new here and to access as well,

The problem I'm having right now is that I made a Query that upon the input of a date, it will search for the last purchase I made and at what cost.

Expand|Select|Wrap|Line Numbers
  1. SELECT [Purchases Order].ProductID, Max([Purchases Order].PurchaseDate) AS PurchaseDateOfMax, Last([Purchases Order].UnitCost) AS CostOfLast
  2. FROM [Purchases Order]
  3. WHERE ((([Purchases Order].PurchaseDate)<[Date]))
  4. GROUP BY [Purchases Order].ProductID;
That works fine, but I want it to work for over a date range between [start date] and [end date]. With this current query it will always just give me the latest purchase with [end date] as frame of reference.

What I would like to do is to have it run for each day over that date range. If it's between 5/1 and 5/3, it'll run it 3 times at 5/1 5/2 and 5/3, and display these values.

Any ideas? Will this require programming?

Thanks in Advance
May 9 '09 #1
12 2185
Denburt
1,356 Recognized Expert Top Contributor
If you want it to run three times then yes I could see some coding involded.

May I suggest dropping the Totals and use a basic sql select statement:

Expand|Select|Wrap|Line Numbers
  1. SELECT [Purchases Order].ProductID, [Purchases Order].PurchaseDate, [Purchases Order].UnitCost
  2. FROM [Purchases Order]
  3. WHERE ((([Purchases Order].PurchaseDate) Between #5/1/2009# and 5/31/2009))
May 10 '09 #2
FishVal
2,653 Recognized Expert Specialist
What about grouping results by [ProductID] and [PurchaseDate]?

Regards,
Fish.
May 10 '09 #3
jamieboy86
4 New Member
OK thanks for the tips. Using that query, I'm able to list all the purchases I made for a range of dates.

The problem I face now is when i put together another query to match up my sales price and purchase price, it lists out every single combination.

For Ex)
Purchases made on 5/1 5/3 5/7
Sales order #1 on 5/6
Sales order #2 on 5/8

my query results give me 3 entries each for sales order #1 and #2, doing all the combinations.

The result I want, however, is to match up the sales order with ONLY the last purchase date before my sales date:

Sales order #1 with 5/3 purchase
Sales order #2 with 5/7 purchase

Essentially, what i want is the LIFO accounting method.

Thanks again.
May 10 '09 #4
NeoPa
32,556 Recognized Expert Moderator MVP
So, you want to look backwards from a set date, to the previous transaction for a product. You also want to repeat this for successive dates in a range.

Will it not be likely that, for any given product, the date may be the same for each repetition? If the last date was not within the range of dates being run for, this will be the case. Is this what you require?
May 11 '09 #5
Denburt
1,356 Recognized Expert Top Contributor
What are you using as a reference between the Purchases and sales? Would this be the productID a Customer or some other reference?

There are lots of ways to accomplish this but I am just gonna through this one out there it may not be perfect it's just freehand and untested:
Expand|Select|Wrap|Line Numbers
  1. SELECT [Purchases Order].ProductID, [Purchases Order].PurchaseDate, [Purchases Order].UnitCost
  2.  FROM [Purchases Order]
  3.  WHERE ((([Purchases Order].PurchaseDate) > #5/1/2009# and ([Purchases Order].PurchaseDate) <(Select Max(SalesDate) From Sales Where sales Order=1)))
May 11 '09 #6
jamieboy86
4 New Member
Thanks to you guys I've got it to work finally. But there's one last tweak i was hoping you guys might be able to help. This query comes out perfectly provided that I always enter my purchase order in order ( 5/2 5/3 5/4). If I happen to "forget" to record a purchase order from a few days ago(5/1), it all unwinds.

Now when this happens for a sales order (eg 5/5):
My PurchaseDateOfMax still correctly displays the most recent purchase date (5/4), however my price because it is a LAST function, I realized that it is the LAST recorded entry, and it will incorrectly provide me with the LAST recorded entry cost which will be (5/1 entry) and not (5/3 entry).

Is there a way to not use the LAST function for price but instead make it search for the corresponding field value in the record entry of the PurchaseDateOfMax?
I've tried just simply using GROUP BY, but then it just lists all the combinations (cost at 5/1 5/2 5/3 5/4) with that sales order.

Expand|Select|Wrap|Line Numbers
  1. SELECT [Sales Order].OrderDate, Customers.CompanyName, [Sales Order].QuantitySold, [Sales Order].UnitPrice, Last([Purchases Order].UnitCost) AS UnitCostOfLast, Max([Purchases Order].PurchaseDate) AS PurchaseDateOfMax
  2. FROM Customers INNER JOIN ([Sales Order] INNER JOIN [Purchases Order] ON [Sales Order].ProductID = [Purchases Order].ProductID) ON Customers.CustomerID = [Sales Order].CustomerID
  3. WHERE ((([Purchases Order].PurchaseDate)<[OrderDate]))
  4. GROUP BY [Sales Order].OrderDate, Customers.CompanyName, [Sales Order].QuantitySold, [Sales Order].UnitPrice
  5. HAVING ((([Sales Order].OrderDate) Between [Start Date] And [End Date]));
  6.  
May 11 '09 #7
Denburt
1,356 Recognized Expert Top Contributor
Glad that we could be of help now lets see if we can help with this next part.

Yes it does look like your getting there. One way would be to leave out the "purchase order unit cost" in this query create a new query based on this one and add the purchase order table again, then join the two on the appropriate joins (Dates, customer etc.) and you should have the results you require.

Another method would achieve the same results although MS Access doesn't always like nested SQL statements again this is typed freehand and not tested but should give you the results required:
Expand|Select|Wrap|Line Numbers
  1.     SELECT [Sales Order].OrderDate, Customers.CompanyName, [Sales Order].QuantitySold, [Sales Order].UnitPrice, (Select ([Purchases Order].UnitCost) From [Purchases Order] Where ((PurchaseDate =#PurchaseDateOfMax#) and ([Purchases Order].CustomerID = Customers.CustomerID)) AS UnitCostOfLast, Max([Purchases Order].PurchaseDate) AS PurchaseDateOfMax
  2.     FROM Customers INNER JOIN ([Sales Order] INNER JOIN [Purchases Order] ON [Sales Order].ProductID = [Purchases Order].ProductID) ON Customers.CustomerID = [Sales Order].CustomerID
  3.     WHERE ((([Purchases Order].PurchaseDate)<[OrderDate]))
  4.     GROUP BY [Sales Order].OrderDate, Customers.CompanyName, [Sales Order].QuantitySold, [Sales Order].UnitPrice
  5.   HAVING ((([Sales Order].OrderDate) Between [Start Date] And [End Date]));
May 11 '09 #8
NeoPa
32,556 Recognized Expert Moderator MVP
@jamieboy86
It's certainly about using Last() instead of Max().

One way to use Max() in this scenario, when you want a value in a record associated with the Max value of another field (as in this case), is to join the fields together in a string, call Max() for this value, then extract the required data out of the resultant max value, and convert it into its original format (date, number, etc). You need, for this to work, to understand the data you're working with :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Sales Order].OrderDate,
  2.        Customers.CompanyName,
  3.        [Sales Order].QuantitySold,
  4.        [Sales Order].UnitPrice,
  5.        Val(Mid(Max(Format([Purchases Order].PurchaseDate,'yyyymmdd') &
  6.                    [Purchases Order].UnitCost),9,99)) AS LastUnitCost,
  7.        Max([Purchases Order].PurchaseDate) AS MaxOfPurchaseDate
May 11 '09 #9
jamieboy86
4 New Member
Thank you Denburt and NeoPa. Your suggestions worked beautifully. Finally I can move past this problem that I've been stuck on for the past week!

Cheers!
May 12 '09 #10
NeoPa
32,556 Recognized Expert Moderator MVP
Pleased to have helped JamieBoy :)

Welcome to Bytes!
May 12 '09 #11
Denburt
1,356 Recognized Expert Top Contributor
Glad we could help jamieboy. Interesting solution NeoPa nice work.
May 12 '09 #12
NeoPa
32,556 Recognized Expert Moderator MVP
Thanks Den.

I use it a lot, but it is limited to whatever can be contained within a string value. I don't find too many problems mind you, but it's still a bit of a kludge I know.
May 12 '09 #13

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

Similar topics

1
by: Michelle | last post by:
Hi all I am having problems creating an update query. I have 2 tables, tblPublishRoster and tblCCAgents_Changed_Shifts. I want to select all records from tblCCAgents_ChangedShifts where...
3
by: Don Sealer | last post by:
I'm guessing this is pretty simple however not simple enough for me. I'm developing a database to track expenses, income, banking transactions, etc. I have a very simple query with four fields,...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
19
by: eskelies | last post by:
I have two queries. My goal is to pull a range of numbers that fall on or in a certain date range. For example, $100 on 9/1/2007 and $200 on 9/24/2007. I want the date range to pick up both the...
1
by: Dan2kx | last post by:
Hello, have a very big list of records that relate to a date and time (seperate fields) and i am using a crosstab to pick out date on the column and times on the row, the value is the count of...
5
by: jennwilson | last post by:
Using Access 2000 - I have a query that is suppose to return the records from table within specified time range and find matching data from another table . Table houses Clinician name, location...
9
by: QCLee | last post by:
Sir can you help me to transfer my Access Query to MS excel? i have a command button on the form to export the parameter query named "HVACWindwardQuery" to excel spreadsheet and i got the codes...
0
by: mastahyeti | last post by:
Ok, i have been racking my brain for the past two days trying to figure out how to write this query. I have a table with dates, client id's, and client level. each client shows up many times and the...
19
by: phill86 | last post by:
Hi I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread. here is the...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.