Data manipulation in MySQL using window functions - mysql

I've a table in my database with record of an agent. The record looks like
Here, Site ID is a place where the Agent 1001 visits at logs himself in at TimeIn and then logs out of that site at TimeOut and raises some tickets and its corresponding amount. Now, If the site is not changed then I want to aggregate these records as shown in the table given below:
I am using MySQL database to do this. Using window functions I can find out consecutive matching Site ID and then mark it as some flag_variable (let's say 1) in a new column.
Now, for these rows where flag=1, I want to update current row TimeOut = next row Timeout. For three or more consecutive record of same Site ID this approach will not work, then we should need a recursive approach I guess.
My question is how can we do this update in the current table and remove redundant records in the table using MySQL syntax.

Related

Is there a way to know the number of records added into the SQL database after a particular date and time

The table doesn't have any date time column. I want to if there is any inbuilt keyword which can does that.
I want to know all commits done after a particular date.
If flashback is enabled on the database you can get records on the table in an around a particular date range in Oracle.(It purely depends on if its enabled and for how long the flashback needs to be kept)
You can query to see the data in the table as of 3 days back as follows
select *
from table as of timestamp sysdate-3

New records since last query in MySQL View

I am looking for a way to create a View that when queried will automatically only retrieve new records since the last query. My tables have a timestamp field for all entries, so for a simple example I can
SELECT * WHERE timestamp >= 'blah'
but I don't know how to determine what blah should be from the last query. So if the View was queried at 11:00 and then again at 12:00, the query at 12:00 should only return records added since 11:00. And so on... This all needs to be accomplished in the View, the end user should simply be able to query the View and get the results.
Is this possible?
There are two ways:
Store last access date time in database per user persistent session
table, if you have one. On next view call to database, use the
previous latest access time in the session to filter rows starting
from.
Store last access date time in user virtual session at client
environment. On every call to server, send last access date time as
well. So that server uses it to filter rows starting from.
I prefer to use second option that process won't write any data in database tables.
As there may be an unread record that slips through undetected (say it came less than a second since the last one accessed, so it has the same timestamp), set a column to auto increment (typically labelled id) and check for entries using it e.g. in PHP save the last accessed record in a $lastId variable, and use:
$sql="SELECT * WHERE `id` > '$lastId'";

Preventing 2 users from updating the same record simultaneously

I have a table tbl_orders. It has a quantity field quantity.
In some part of my application I need to decrement the quantity by 1.
I already know the id of the record (available from the client side), so I issue an update statement:
UPDATE tbl_orders
SET quantity=quantity-1
WHERE id= 6
The problem is that this query can accidentally be run multiple times concurrently.
For example, 2 customer service operators may update the same record simultaneously.
That means that the quantity will be decremented by 2 when it is supposed to be decremented once only.
I tried putting the update in a transaction, but that resulted in only delaying the second transaction until the first one was committed. Once it was committed the second update ran and decremented the record again.
How can I make sure that other queries fail if one is modifying a record?
UPDATE:
for an update to be valid the quantity on the client side was the same in the database. For example if a user sees a quantity 5 on his browser and wants to decrement it, the value in database must be the same.
UPDATE 2
I found a good explanation here for optimistic locking using Doctrine 2:
One approach I've used/seen in the past was having a timestamp column. When querying, ensure you have both the ID and the original timestamp at the start of editing the record. Then, when you are trying to update, send via
update YourTable
set counter = counter -1,
TheTimestampColumn = new timestamp value
where ID = yourID
and TheTimeStampColumn = timeStampEditStartedWith
This way, whoever gets to it first with the original starting timestamp would win. For the one following, you would have to track how many records were updated, and if that count equals zero, then you would notify the user that another person made a change to the record while you were viewing it. Do you want to reload the latest data? (or something like that).

Is it possible for MySQL to "auto-update" a field whenever changes are made in another, unrelated table?

Let's say that we have one table with a field called sales_total and another table with a bunch of sales entries. Let's also, for a moment, imagine that it is impractical to count the entries every time we want to see the total number of sales.
Is it possible to have MySQL automatically update the sales_total field every time the number of sales entries changes?
I know that you can do this by running another query via C#, PHP or whatever - I'm just curious whether MySQL (or some other database system) can do this within itself?
P.S. This is of course a pretty banal example - the ideal solution should be able to handle more complex operations (storing several rows as a string in a field, etc).
use mysql trigger...
trigger on update from the first table should have some queries to update the second table.

Microsoft Access 2010: Update a field in another table on button click

Basics about the database
I am working on a (relatively) simple database that stores inventory data. I am using Microsoft Access 2010 in order to do this. I have six tables with the following relationships:
Relationships of Database
I have created forms which combine the Transaction table with Ordered, Received, Allocated, or Dispensed. Each form requests an amount which will then be used to update On Hand, On Order, or Allocated (from the Material table) respectively.
The Problem
For example, my form to update Transaction and Order should be able to take in the Amount ordered, save all the data from the fields to the Transaction and Order tables as well as add the amount from Amount to On Order in the Materials table.
I have been working on this database for the past two days. I have searched several times for possible ways to perform a similar function, but have come up with nothing. All the tutorials I have found which seem remotely close to what I need to accomplish are for versions of Access which are much older than 2010. Unfortunately I have had little experience with the actual coding within Access, so I am stuck clicking around within the buttons on its menus.
What I have tried
Currently, the program is set to run the following Update query:
Screenshot of update query
This query works if I have one Material stored in the database but adds all the Amount values from Ordered to On Order every time it is ran, which is unfortunately not what I need it to do. I only need each Amount value added to On Order once.
You need to relate the Ordered and Material tables by adding a foreign key field to the Material table, ex. OrderedFK (Long Integer). This new field must be updated whenever a row is inserted into the Ordered table (assuming the "No" Field is AutoNumber). This is typically performed by using a Form (Ordered) and Sub-Form (Material) and setting the sub-form' Link Master (No) and Link Child fields (OrderedFK).
You can then join the Ordered and Material tables on the Update Query to achieve the desired result.