MySql : splitting row based on one column and manipulating other rows - mysql

database table screenshot
I have a table as shown in pic with 10+ columns with number of poeple. I want the duration to be in 15 mins.i.e splitting 10:00-10:30 row to 10:00-10:15 and 10:15-10:30 and so on.
The column value should be divided by 2. Any pointers?

I would store the duration as a datetime for just the beginning of the duration. I am assuming your duration is a varchar or something now.
alter table your_table add duration_time datetime
update your_table set duration_time = cast(concat(date_field,' ',left(duration_field,locate('-',duration_field))) as datetime)
and then always assume 15 minutes with the code that reads the table. You can then create a mysql query
insert into your_table
select (every field..., date_add(duration_time, interval 15 minute) as duration_time from your_table
will duplicate every entry and add 15 minutes.
Then when you query your data you can format the time part as time-time+15 minutes in your python code

Related

how to update a sql database table based on the condition from another table in the same database

I have a two tables in a database.
table_1(device_ID, date,voltage)
table_2(device_ID,device_status)
I am trying to create an event to execute every 5 minutes.
What I am trying to achieve is, select device_ID from table_1 if there is no new data over the last 10 minutes and update the table_2, that means set device_status to 0.
How do i pass conditions between two tables?
BEGIN
select device_ID from table_1 where date = DATE_SUB(NOW(), INTERVAL 10 Minutes);
//here i will get device_IDs if there was a data within last 10 minutes.
//but i need device_ID if there were no data.
//how to update table_2 based on the above condition?
END
You can use the results of your first query as a subquery to de-select rows (by using NOT IN) for the UPDATE:
UPDATE table2
SET device_status = 0
WHERE device_ID NOT IN (select device_ID
from table_1
where date > DATE_SUB(NOW(), INTERVAL 10 Minutes))
Note I think you probably want >, not = in your where condition in the subquery.

fetch last 5 minutes of data from oracle table

Hi i am trying to fetch last 5 minutes of data from oracle table.The query is written below and its not working somehow.
select * from mytable where (time_to_sec(timediff(now(),mytable.time_stamp)) <= 300)
Its showing this error ORA-00904.
I tried one more query.
select * from mytable where TIME_STAMP > (sysdate - numtodsinterval(5,'minute'))
Now, can you tell me the query which fetches data of last 5 minutes and which deletes data that is in the table for more than 12 hours.Thanks.
I need queries in both oracle and mysql. The mysql query i tried is here.
delete from mytable where (time_to_sec(timediff(now(),time_stamp))/3600 >12);
In oracle subtracting 1 from timestamp means one day. And You can substract a fraction of one. So,
current_timestamp - (5/(24*60))
gives You date from 5 minutes ago. Using that we can query:
select * from mytable where TIME_STAMP > current_timestamp - (5/(24*60)
Which should give You needed result. I find this method more straightfoward and simpler to remember than using special functions.
If You want filter out data from last 12 hours than You can query it like this:
select * from mytable where TIME_STAMP <= current_timestamp - 0.5

mysql, need to update column with random dates

I have a 'timestamp' type column in my table called updated_date. When adding a column to the table, all rows got updated to the same updated_date. Not a disaster as we're still in testing, but it kind of broke the functionality of our site (which shows things in order of updated_date).
Is there a way I can change all the updated_date values in the column (but where id is lower than x) to some random date (or an incremental date)?
Thanks in advance!
This might solve your problem:
UPDATE updated_table SET timestamp = FROM_UNIXTIME(1e9 + id) WHERE id < x;
Basically it sets dates to Unix timestamps corresponding to 1 billion + id (1,000,000,000 unix timestamp is 2001-09-08 21:46:40). That way you get unique timestamps in order of id.
Well, you could do this
UPDATE table SET updated_time = NOW() WHERE id < x
Given id belongs to table
in case you want some random data from the past
UPDATE test2 SET update_time = NOW() - interval rand()*120 day - interval rand()*36000 second WHERE id < x
Tweak it to your needs
Timestamps are just the number of seconds since the epoch (1970-01-01 00:00:01). If you start with a base timestamp, you can just add a random number of seconds since that number and you have random dates.

Mysql updates based on time?

Is there a way automatically update some tuples based on time.
I have a field that I would like to increment every week from the time stored for that particular row.
Say I have two tuples with date and count fields:
2000-01-02 10
2000-01-03 1
Is it possible to automatically increment the count field every week from the stored date?
So that the first row is incremented on 2000-01-09 and the second row is incremented on 2000-01-10 and this would be done weekly.
Or in general can I update something automatically based on some time gone by?
Thank you.
You could store an extra field: next_increment_date.
Then you update regularly (say, once per hour or day... or however often makes sense):
UPDATE my_table
SET next_increment_date = DATE_ADD( next_increment_date, INTERVAL 1 WEEK ),
count = count + 1
WHERE next_increment_date <= NOW();
I think you want the event scheduler:
http://dev.mysql.com/doc/refman/5.1/en/events.html

MySQL : selecting data based on a timestamp interval

My dataset is a table with 3 rows : ID of a hard-drive, percentage of empty space, timestamp. The table is appended with the new state of each HDD (1200 of them) every 20 minutes.
If I want to pick the last state of my HDD pool, I go for a MAX(timestamp), and a MIN(timestamp) if I want the oldest.
But say I have a given timestamp, how can I ask MySQL to retrieve data from more or less X seconds around this timestamp ?
WHERE yourTimeStamp
between TIMESTAMPADD(SECOND,-3,yourtimestamp)
and TIMESTAMPADD(SECOND, 3,yourtimestamp)
where -3 and + 3 was substituted for X
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampadd for more help.
Like this:
WHERE timestamp BETWEEN DATE_SUB('<given_timestamp>', INTERVAL 5 SECOND)
AND DATE_ADD('<given_timestamp>', INTERVAL 5 SECOND);
As mentioned in the other answer, your query is slow when selection is based on the timestamp field.
You can add an INDEX on that column to speed it up:
ALTER TABLE <table_name> ADD INDEX(`timestamp`)
Note that, depending on the size of your table, the first time you add an index it takes a while. Secondly, it slows down INSERT queries and adds to the size of your database. This is different for everybody so you just have to find out by testing.