Merging several rows based on conditions in MySQL - mysql

I have a large database containing informatiom about orders. Each order has a unique number, a tripnumber it is assigned to and a location number.
It sometimes happens that multiple orders are delivered to the same location, during the same trip. I want to merge these entries into one, as they are skewing my analysis results.
I want to iterate over the entire table, checking in every trip whether there are orders that have the same location number. If so, I want to update the rows in MySQL to either add together the values in the columns of that row or take the maximum of the two.
Is this possible in just MySQL?
I am fairly new to using MySQL (and coding in general) so I'm not sure how to write anything that iterates in it.

Related

How to implement a MySQL table that the row order can be customized? [duplicate]

This question already has answers here:
Making changes to multiple records based on change of single record with SQL
(3 answers)
Closed 5 years ago.
I'd like to save some data in a MySQL table. The data have their orders which can be modified at my will. Say I have 10 rows. I want to move the 10th row to the 5th position, or insert some more rows between 2nd and 3rd position. Then, with a viewer I can get the data with the order I set. How can I implement such a table?
As I thought, I would save the order as float number in a new column. Each time I change the order, say, move 10th row between 5th and 6th, I would get the order number of 5th and 6th and get the average number of them, and update the order column of 10th row. Then I can get the data that ORDER BY order column. But I don't think it's a good idea. Any help about this problem?
You don't order the table, that makes no sense. You are actually not interested in the order the entries are placed in that table. Consider it random.
You are interested in the order you want to see the entries in. For that you create an additional column, call it "select_order" or "priority", however you like. In that you store simple integers which you use to describe the order you want to see the entries in.
Now you can "re-order" the entries however you like by changing those numbers in that order column. At query time you add an ORDER BY select_order clause to your SELECT query and will receive the entries in exactly the order you want.
This is the standard approach for relational database models. Which does not mean that there are no other approaches that might be interesting to look into for very special situations:
a priority table instead of a column which is joined during the SELECT query. This might make sense for situations with much more write than read operations. Note the much however.
a multiple column approach for situations where you can group entries and only re-order inside such groups. That dramatically reduces the number of entries you have to updated in case or re-ordering.

In MySQL, is it more efficient to run my calculation everytime I query the database, or save the values in a new table?

I have a 2-D table of values that I am running through an algorithm to filter out the zero values from the front columns and back columns, and then taking the average across the rows.
In addition to doing this, I am sorting the averages by another column's values.
So in the end, I need a 2xN table with ordered values in my first column and the averages in the 2nd column after filtering out the zero values.
Is the best implementation for this running a routine every query? Or would it be okay for my to run through these computations and then create an entirely new organized table of these values with a foreign key to link it to the other?

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.

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.

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.