I have to generate a rolling sum or Cumulative sum of the OIL field for each WELLNMBR as per its MONTH value.
The query used is
{SELECT tblProductionData.WELLNMBR, tblProductionData.[Month], tblProductionData.DAYS, tblProductionData.OIL, tblProductionData.GAS, tblProductionData.WATER, DSum("[OIL]","tblProductionData","WELLNMBR=" & "'" & [WELLNMBR] & "'" & "AND MONTH <=" & "#" & [MONTH] & "#") AS Expr2
FROM tblProductionData;
In the result the sum changes after every year, not after a month. I would like to know the reason for this strange behavior and the method to obtain the rolling sum in every month.
Incorrect result
http://i.stack.imgur.com/NFBRm.png
Desired result
http://i.stack.imgur.com/U8I9r.png
Your query begins '{'. This needs to be removed. Also 'MONTH' is a reserved word. Try replacing it with 'Mth', then proceed from there.
Related
I am trying to create a query which will update a blank "To Date" field with the day prior to next updated date.
Example, an [Item Number] standard cost was updated ([From Date]) on 01/03/2019, then again on 01/07/2019 and then again on the 01/01/2020.
I would like an adjacent column which is updated with [To Date] of 30/06/2019, 31/12/2019.
I will run a subsequent query which updates blanks (i.e. current cost as there is no next [From Date]) to Today End of Month date (I assume I need a separate query for this rather than an IIF which can populate Blanks as part of this update query?)
Currently I have below, but it is updating the [To Date] with day prior to the newest date in all instances (i.e 31/12/2019 for first 2 rows), I understand that I need a SORT within the below query:
Many thanks in advance from this first time poster!
UPDATE
Standards
INNER JOIN
Standards AS Standards_1
ON
(Standards.[Item number] = Standards_1.[Item number]) AND (Standards.[From date] < Standards_1.[From date])
SET
Standards.[To Date 2] = Standards_1.[From date]-1;
This might work, but the performance migth be a bit slow:
UPDATE Standards
SET [To Date] = Nz(
DateAdd("d", -1,
DMin("[From Date]", "Standards", "[Item Number]=" & [Item Number] & " AND [From Date] > #" & [From Date] & "#")
)
, DateSerial(Year(Date()), Month(Date())+1, 0)
)
Start from the DMin function, which will lookup the next date greater than the current date for the item.
DateAdd function will subtract one day from this date.
Nz will use the value returned by the DateSerial function if the DateAdd function result is Null.
DateSerial function returns the end date of the current month.
# are added because of the date value of the [From Date] field in the criteria of the DMin.
Instead of using the [From Date] field in the DMin function criteria, you can consider using the Autonumber Primary Key field. You'll need to remove the # in the criteria.
Everything between Nz(...) will need to be on one line.
This solves both of your requirements, just test the performance.
I have two datasets:
dataset1 - which is the result of a large select statement.
id
name
date
total_orders
dataset2 - which is result of another select with different tables than first dataset.
id
name
date
total_cost
My goal is to get just the total cost from the dataset2 by using the id, name and date from dataset1. The tables used for dataset2 are not accessible to dataset1. I have not seen where I can join the two datasets, however, I did see a video that shows concatenating the fields in a lookup statement,
=lookup(fields!id.value+fields!name.value_fields!data.value,
fields!id.value+fields!name.value_fields!data.value,
fields!total_cost.value, "dataset2")
But this is not work for me.
Any help is greatly appreciated.
Thank you.
Try this:
=Lookup(fields!id.value & "-" & fields!name.value & "-" & fields!date.Value,
fields!id.value & "-" & fields!name.value & "-" & fields!date.Value,
fields!total_cost.value, "dataset2")
Let me know if this helps.
I have a date based query that returns two fields, Week Ending Date and L2N, and I want to add a third field that provides a rolling total of the L2N field. I have tried using DSum as follows:
RunL2N: DSum("[L2N]","Occupied Apts & L2N", "Week Ending Date=" & "'" & [Week Ending Date] & "'")
In the code above, L2N is the field I want to sum, and Occupied Apts & L2N is the query that returns the fields. The query asks for a Week Ending Date and then delivers all of the records that equal or proceed the given Week Ending Date.
It no worky right. My goal is for the RunL2N field to show a rolling total of the L2N field for each record. In other words, if the query returns multiple records, I want it to show the L2N field result, and then show the Run2L2N field, which sums the L2N fields of the records above and the current record.
So if the query returns a 1 in the L2N field, then a 3 for the next record, then a 5 for the next record and lastly a 7 for the final record, I want the RunL2N field to show 1 for the first record, 4 for the next record, 9 for the next record and lastly 16 for the final record.
Since the field name includes spaces, bracket it like this: [Week Ending Date]
Assuming it's Date/Time type, use # delimiters before and after the date value.
Finally I think you want to get the sum from rows where [Week Ending Date] <= the date in the current row.
DSum("[L2N]","Occupied Apts & L2N", "[Week Ending Date]<=" & Format([Week Ending Date], "\#yyyy-m-d\#"))
However if you use a correlated subquery, instead of DSum(), to compute the running sum of L2N, you won't have to bother about delimiters for the date value.
SELECT
y1.[Week Ending Date],
y1.L2N,
(
SELECT Sum(y2.L2N)
FROM [Occupied Apts & L2N] AS y2
WHERE y2.[Week Ending Date] <= y1.[Week Ending Date]
) AS RunL2n
FROM [Occupied Apts & L2N] AS y1;
I'm having some problem with this query, it works on my localhost (IIS 6) but when i uploaded the same code to web server (IIS/7.5) it returns no data from the Database.
Here is the query:
Set RsTdL = Con.Execute("Select * From COURSE1 Where DUE_DATE Like '"&Date&"%' and CLASS = '"&CCode&"' ORDER BY SUBJECT, LECT_NO, SUBTOPIC")
'" & Date & "%' matches the system date in the database so it'll show current date record.
And in the DUE_DATE column the date is in this format 9/3/2013 3:42:03 PM
i don't know how to debug this query because its working on my local machine and the WEB SERVER doesn't return any errors so im stuck ...
Please help,
thanks
DUE_DATE is Date/Time type and you want to match all values for today's date ignoring the time of day. So ask for DUE_DATE greater than or equal to the earliest time of today (midnite) and less than tomorrow's date.
"SELECT * From COURSE1" & vbCrLf & _
"WHERE DUE_DATE >= Date() AND DUE_DATE < DateAdd('d', 1, Date())" & vbCrLf & _
"AND [CLASS] = '" & CCode & "' ORDER BY SUBJECT, LECT_NO, SUBTOPIC"
Also CLASS is a reserved word, so enclose that name in square brackets.
The Database may be using the date format set on the server.
Which could be different than your locale machine. Do a simple select on the date fields in each environment to see how they look.
For safe date comparisons use the datediff function and use d ( which counts the number of days ) as the parameter. Select rows with a datediff of zero to retrieve all courses on the same date. This will work even if the datestamp includes the time.
So i have the following query:
DoCmd.RunSQL "delete * from [TABLE NAME] where month = '" & Format(PrevMonth, "yyyy-mm-dd") & "'"
month is a Text field.
Lets say PrevMonth = August 2010 so instead of delete the rows where the date is august 2010, i want it to delete august 2010 and all after that? so september 2010, october 2010, and so on.
thanks.
You can use the CDate() function to cast your text date to date/time data type.
Then:
DELETE FROM [TABLE NAME]
WHERE CDate([month]) >= #2010/08/01#;
I enclosed the field name in brackets because Month() is an Access VBA function ... the brackets let Access know to treat month as a field rather than the function. If it were my database, I would rename the field.
First create a query based on [TABLE Name] where you convert month from text to date
SELECT *,CDate("1 " & [Month]) AS DateDate
FROM Table Name;
Now you can use the dates as normal
DELETE Query1.DateDate
FROM Query1
WHERE (((Query1.DateDate) Between #1/1/2010# And #12/31/2010#));
Hope this helps