office access substrac values from each row in one field - ms-access

Please help, how to substract values in one field from one row to another, for example:
I want it become like this:

This Question is not very clear but I've given what you have asked for.
This will place the first total in the row with NO = 1 and then will reduce that total as NO increases.
(Select Total from table1 where Date1 = (Select Min(NO) from Table1))-IIf([No]=1,0,(DSum("[Total]","[Table1]","[NO]<=" & [NO] & " AND [NO]<>1")))
Add this as the last column in a query on your table. You will have to replace "Table1" with your table name and you should really change the column names to be something different than NO and Date as these are used for other things in Access.

The way you have data stored could make this impossible because there is no way to distinguish which records should be subtracted from which records. That is assuming there will be more than one record considered a positive (increase) entry. Options:
Another field that identifies record as either Increase or Decrease
Enter the values to be subtracted as negatives
Regardless of which you choose, a report is the easiest way to show a running balance because textbox on report has RunningSum property.
But if these records are not incoming and outgoing transactions and what you really want is for each record to get value from previous and subtract, then review Stackoverlow - get previous value
Why do you name fields with Kmh/mph, those values are obviously not Kmh/mph, they look like odometer readings. Better field names may be OldOdometer, NewOdometer. However odometer readings would increase, not decrease. Why do you need to add new to old? Just what is this data?

Related

SSRS - Get the max time for the max date from a list of dates

I have an SSRS report with a dataset that queries AS400. I am trying to print a "Version number" on the top of my report. Due to several complications, I cannot really print a version number better than last update time.
This is my what my data looks like:
The query takes a parameter that is a PackingDate. As in, the ShopOrders were written to pack on this date.
The query returns anywhere from 10-25 shop orders for each PackingDate.
Each of these shop orders have 4 columns: DateCreated, TimeCreated, DateModified, TimeModified.
Shop orders go through changes and revisions frequently. Every time a shop order is changed, the DateModified, TimeModified field changes.
I want to look at each of these shop orders, look at the DateModified, get the maximum date, then look at the TimeModified, get the maximum time, and add a concatenated form of that as the version number on top of my report. For instance:
Date fields are in yyyyMMdd format and time fields are in mmhhss format.
ShopOrder: 65642
DateModified: 20180118
TimeModified: 124500
ShopOrder: 65643
DateModified: 20180117
TimeModified: 142000
Since the MAX(DateModified) in these two shop orders is 20170118, I want the TimeModified for that corresponding date: 124500.
So the version number would look like this: v0118.1245.
I would like to, if possible, have this done in SSRS and not have to do much in my dataset, but that is not written in stone. I just want the MAX(Time) for THE MAX(Date).
EDIT 1:
This is what I've already tried:
LOOKUP(MAX(Fields!DateModified.Value), Fields!DateModified.Value, MAX(Fields!TimeModified.Value), "ShopOrders")
I was pretty proud of myself for thinking of this, but that burned down quickly when I got an error that said I cannot use Aggregate functions in Lookup.
I'm not sure if you can use lookups in this case although I could be very wrong as I don't use lookups enough to know their limitations.
The way I would approach it would be to simply add a new column in your query results that combines your date and time columns. Then you could simply get the Max of that new column.

how to populate column from the content of other columns after a row is inserted in the same table in mysql

I have a table traffic with 7 columns, namely toll_id, date1, shift, car_single, car_return, car_local and car_total.
How could I populate first 5 columns manually, and then store a value in column car_total, which will be the sum of car_single and car_return?
Here is the image of my table:
Just to add a 3rd and 4th ways of achieving the desired outcome:
If you have at least MySQL v5.7.6, you can use a generated column as car_total.
Alternatively, you can choose not to store car_total at all, but calculate this value on the fly while querying the table.
Having a column to store the results of the calculation is good if you regularly have search based on that field because you can use indexes to speed up the searches. Calculating the results on the fly may be better, if you just need to display the result of the calculation, but there is no need to filter on it.
There are two ways to do this:
Add the logic in the application itself, so that it calculates total before inserting the record. (Recommended)
Write an after insert trigger (http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html) which calculates the count when record is inserted.

Selecting a period in field based on range

Thank you all in advance for any help. I'm Still very new to access and have no idea where to start to find a solution.
What I am trying to do is to auto populate a field in my table called "Period". I would like to have it use the "Activity_Date" to look into a different table that has date ranges that reference to the correct period. Based on which "Period" the "Activity_Date" falls under will return the correct "Period". I've tried using calculated data type and queries and I feel no closer to an answer than when I started.
Thanks again for your time.
I would question why you NEED to populate the field period in your table.
In short, I wouldn't bother.
The period it is in can be derrived from the activity date field that is in the same record.
So you can write select statements that calc the period for the record in your MyTable as required.
SELECT TableWithPeriods.period, MyTable.activity_date
FROM MyTable
LEFT JOIN TableWithPeriods
ON MyTable.activity_date
BETWEEN TableWithPeriods.StartDate
AND TableWithPeriods.EndDate
If you need to access the period a lot then there is an argument for keeping a period value in the MyTable in step with the TableWithPeriods.
Keeping in step could be akward though as what if someone changes one of the period 's dates?
Keeping in step might mean writing a bit of SQL to update ALL MyTable rows that wither do not have the period set or when the period is now different.
A VBA update statement will look a bit like the SELECT above.
Or
you could use database the onchange macros that respond to data being added or updated in the MyTable (and the TableWithPeriods, if users can change dates).
Anyway, there's my opinion. I would NOT copy the value over.
PS I'm not 100% sure about the SQl I gave above, this might work though
SELECT TableWithPeriods.period, MyTable.activity_date
FROM MyTable
LEFT JOIN TableWithPeriods
ON ( MyTable.activity_date >= TableWithPeriods.StartDate
AND MyTable.activity_date <= TableWithPeriods.EndDate )

Design - Microsoft Access - Unique "Serial" Number

I am looking for some design techniques to achieve the following:
A 3-part serial number that is generated upon record entry. Format Example: 25-001-14
The number is used to track yearly records from various locations.
The first part states the location the record is associated with, this would be a user input during record creation.
The second part is the record number, I would like for this to be automatically generated, but needs to be sequential and separate for each location and needs to reset each year.
The third part is the two digit number for the year the record was created in. I would like this to be automatically generated if possible. Note: I am currently not concerned with when this cycles back around and I face redundant data issues.
I'm thinking I would like records to be stored in multiple tables that are separated by location, if this would help things!
Any ideas would be greatly welcomed.
I think I would use 3 key fields - one field each for location, record and year. The location and year fields would be created when you get the input to create new records. I would set up a query to find the last record number used by location and year and use that query to assign the new record number when you create a new record. The concatenation of the 3 fields would be the key you described.
With a key field for location, separate tables are not necessary unless that's useful for other reasons. I would probably use just one table - you can always filter the records by location anytime you need to.

How do I select a specific column based on a variable in a MS Access query?

I have a large table with the following fields:
Date
Product_ID
AmountEUR_Field1
AmountEUR_Field2
AmountEUR_Field3
AmountEUR_Field4
AmountEUR_Field5
where each AmountEUR field represents the sales amount for a product.
The reason for having 5 different AmountEUR fields is that they are based on different Currency Rates (in example BeginingOfMonthRate, AverageMonthRate, EndOfMonthRate etc.).
I now want to copy a specific AmountEUR field to another table, but the AmountEUR field to be copied varies over time (sometimes it is AmountEUR_Field2, other times it is AmountEUR_Field5).
Therefore I need to select a specific column based on a variable from another table. (that variable should then have value between 1 and 5).
I have been thinking about making a new field called AmountEUR_ToBeUsed that is updated with the correct AmountEUR_Field, but that brings me back to the same problem of selecting the specific column I want copied.
Can a solution be made within the Access query designer, or do I need some VBA code?
You can just make this with the Access Query designer.
Specifically you will need the function IIF.
For instance, if you want to specify that before a date you wish to use AmountEUR_Field1, and otherwise AmountEUR_Field5 you can say:
IIF(somedate<#1/1/2011#,AmountEUR_Field1,AmountEUR_Field5)
Note, depending on the settings of your PC, you may have to say:
IIF(somedate<#1/1/2011#;AmountEUR_Field1;AmountEUR_Field5)