Add blank row to MySQL array at a specific point - mysql

I am pulling data from a MySQL DB, it is ordered by the serial number, however I want to add a blank row after one of the fields drops below it predecessor. for example.
Serial No. Hours. type
11111 5 1
11112 7 1
11113 12 1
11114 5 1
What I want is ...
Serial No. Hours. type
11111 5 1
11112 7 1
11113 12 1
Null Null Null
11114 5 1
Because the hours have dropped it add a blank line.
At the moment I use a simple Array to pull the data then loop it.
Thanks.

Related

what is the correct way to select the highest value from column not primary key

I have a table (report) consist of several records and one of them about int values (column) I am trying to get the highest number of the fall_value column the id only primary key, the table as following:
id (P)
fall_value
date
3
1.2
2021-01-29
4
1.5
2021-01-30
5
1.6
2021-01-30
6
1
2021-01-31
7
5
2021-01-31
8
1.5
2021-01-31
9
1.5
2021-01-31
10
14
2021-01-31
11
15
2021-01-31
expected result: 15
I have tried the following inquiry:
SELECT max(fall_value) from report;
I got an unexpected result: 5
and also I got a message saying:
Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available
It sounds like fall_value is a string, not a number, and the string "5" is indeed greater than the string "15".
Try converting to a number. A convenient way is to use implicit conversion:
SELECT max(fall_value + 0)
FROM report;

Algorithm for splitting date according to new record

In my mysql database
I have to write stored procedure.
I have table DATE which has 4 Columns
Table: DATE
Sq number(auto generated) From_Date To_date value
2 20170112 20170115 3
3 20170116 20170220 5
4 20170221 20170301 7
and so on
so if I get data which has to be added to table
from and to as 20170119 and 20170201 and value 6
then my table should look like
Table:DATE
Sq number(auto generated) From_Date To_date value
2 20170112 20170115 3
3 20170116 20170118 5
4 20170221 20170301 7
5 20170119 20170201 6
6 20170202 20170220 5
is there any algorithm or any logic to implement this kind of situation and handle all other possibilities
Not an answer, but too long for a comment...
If it was me, I'd revise the question, e.g.
start_val end_val
3 5
6 11
What would be the effect of bringing the values 4 and 8 into this table?

Get last value in multiple rows MYSQL

I have a table that I use to record the activity log of an application. Now I have to get all records from entity X that have been published in some range of dates. So if a record has been published and later unpublished, it doesn't have to appear in the results.
I don't know how to explain it really, it's find the last appearance of each one and then catch values that are "1" or "0", on depends that I need in each case.
A simplified example (the real table has more fields and more data):
id user_id date model main_relation_id field new_value
1 24 2017-03-21 A 1 publish 1
2 24 2017-03-21 A 2 publish 1
3 24 2017-03-22 A 3 publish 0
4 24 2017-03-22 A 2 update some text
5 24 2017-03-23 A 1 publish 0
6 24 2017-03-23 A 1 update some text
7 24 2017-03-24 A 3 publish 1
8 24 2017-03-24 A 2 publish 0
9 24 2017-03-24 A 2 update some text
10 24 2017-03-25 A 1 publish 1
11 24 2017-03-25 A 2 publish 1
11 24 2017-03-26 A 3 publish 0
I need to get main_relation_id, filtering by model and date, so if I want to get all registers from model A that have been published between 2017-03-21 and 2017-03-24 I'll get:
model_main_relation_id
1
3
and if I want to get all registers that have been unpublished in the same dates, the result have to be:
model_main_relation_id
2
How can I get this result?
So, you would like to filter on the latest status by main_relation_id, whether the particular main_relation_id has been published (field='publish'; new_value=1) or unpublished (field='publish'; new_value=0) within a period.
Since the dates within the date field of the sample data are equal for multiple records, therefore I must assume that a higher value in the id field means later event. Therefore the max(id) per main_relation_id would yield the latest even record.
What I would do is to get the max(id) per main_relation_id within a date range for a particular model in a derived table where field is 'publish' and join this back to your table to find out whether the particular main_relation_id was published:
select table.main_relation_id
from table
inner join
(select main_relation_id, max(id) as maxid
from table
where date>=... and date<=... and `field`='publish' and model='...'
group by main_relation_id) t on table.id=t.maxid
where table.new_value=1
You need to substitute the filter criteria in place of the .... If you would like to get the unpublished data, then replace table.new_value=1 criterion with table.new_value=0.

Sort values in two column and insert order position into another column in mysql

I have a database about sports event that contains:
*User ID
*Amount of Points that the user got on that event
*Time (HH:MM:SS) that took the user to complete track.
How can I first sort them by no. of points, then if two users have same amount of points, by time (shorter is better); and then insert the places to rows?
I have database like that:
ID No. of Points Time Place
------------------------------------
1 15 00:56:00
2 13 00:55:15
3 17 01:00:00
4 17 00:57:00
5 19 00:52:15
I need to have it with places:
ID No. of Points Time Place
------------------------------------
1 15 00:56:00 4
2 13 00:55:15 5
3 17 01:00:00 3
4 17 00:57:00 2
5 19 00:52:15 1
I hope, you understand that. Sorry for bad English.
Best regards,
You can do this with update statement as follows.
SET #placeValue:=0;
UPDATE [Table Name] SET Place=#placeValue:=#placeValue+1 ORDER BY
[Amount of Points] DESC,Time ASC

Stop duplicated indexing

I am trying to stop duplicate entry's into my database (below). eg it will come up with an error message if the vechID, Collection date, and return date is the same. I am opening my table in design view and clicking indexes and then indexing the relevant fields. but it wont work let me and keeps saying no due to duplicate values. is this the correct method
Booking ID VechID CuID Collection date Return date
1 3 7 01/07/2017 10/07/2018
2 1 7 23/04/2017 16/05/2018
3 2 1 17/05/2017 28/05/2018
4 4 2 15/05/2017 20/05/2018
5 5 2 01/06/2017 24/06/2018
6 6 2 22/07/2017 29/08/2018
7 4 8 01/07/2017 15/07/2018
8 8 8 01/08/2017 20/08/2018
9 8 2 21/01/2017 20/01/2018
10 4 8 25/09/2017 02/10/2018
13 8 8 25/09/2017 02/10/2018
Yes, you need to create a unique index on the fields (vechID, Collection date, return date).
Of course you can't do that if you already have data in your table that violates this unique index.
Use the query wizard for Duplicate Search to find and delete them.