I have a MySQL database, I use Heidi SQL to access it. The following columns are part of one of my tables:
START_DATE
START_TIME
STOP_DATE
STOP_TIME
01-10-2022
02:30:00
01-10-2022
03:30:00
What I need is to subtract start date/time from stop date/time and then create or insert into a 5th column called duration (in minutes), in this case the 5th column would get populated with 60.
Is this even possible?
Thanks
See TIMESTAMPDIFF
Since the DURATION column is to be the 5th column, I am assuming that each of the columns above are separated.
Using MySQL, you could try:
SELECT *,
TIMESTAMPDIFF(MINUTE,START_TIME, STOP_TIME) AS DURATION
FROM
TABLENAME;
The resulting query will display all your previous columns and add on the DURATION column as well. You may save the query to a table when you are done.
All of this is assuming that your working columns, as above, are in DATETIME. To check the information about a table's structure: DESCRIBE
Related
I have a table containing years of data but no date or timestamp columns. Now I have to fetch last one year's data. How can achieve that when the table does not have any timestamp or date columns ?
How can achieve that when the table does not have any timestamp or date columns?
In general, you cannot; if you do not have any data inside the table to tell you a date associated with the row then there is not any meta-data that will tell you.
If you have enabled flashback (with a large enough history) on the table then you could compare the state of the table now to the state of the table a year ago using something like:
SELECT * FROM table_name
MINUS
SELECT * FROM table_name AS OF ADD_MONTHS(SYSDATE, -12);
I would like to compare two datetime values from different rows that share a reference number. Both datetime values with the same reference are always from the same day, so the expected result is a column with the number of hours, minutes and seconds that passed between the datetime of each row and the first row with the same reference available in the table.
The problem is that there are usually more than two rows with the same reference and I always need to substract the earliest datetime available for this reference from each row's datetime.
How could I do this in SQL Server?
Best regards
You would simply use a window function. Something like this:
select t.*,
datediff(second, min(dt_col) over (partition by ref), dt_col) as time_in_seconds
from t;
Your question doesn't appear to be about formatting the result as a time, so this just returns the value as seconds.
Is there any way to add a derived date/time column (to an existing table) to hold calculated running time from a race.
The values are in hours, round to 2 decimals. There are 2 columns for start time and end time in the table. Below is the last I've tried got a missing expression error.
ALTER TABLE race
ADD race_time AS (SELECT ROUND(DATEDIFF (endtime, starttime),2)FROM race);
There is no datediff() function in Oracle and you also don't need a SELECT statement to create a computed column:
ALTER TABLE race
ADD race_time AS (endtime - starttime);
If endtime and starttime are defined as timestamp the race_time column will be of the type interval.
If those two columns are date columns, the result will be a number representing the number of days between the two days
It's been a while since I used this functionality but I think it would look more like:
ALTER TABLE race
ADD race_time AS (ROUND(DATEDIFF (endtime, starttime),2))
Note that I'm not aware of a DATEDIFF function in Oracle - if you haven't written this yourself and are looking to get the number of hours between two dates it would be more like:
ALTER TABLE race
ADD race_time AS (ROUND((endtime - starttime)*24.0,2))
If your columns are timestamps, it would probably be easier to convert them to dates when doing the math
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=3ac1f46f8d7296754d14a3de6191dbe8
I am trying to write sql query for the following scenario. It would be great if can get any help on it.
Scenario :
I have a table(emp) which has three columns id,time and value. There is no primary key in the table. time is a date column holding date and timestamp.
Table will be started to update from afternoon onwards. So table will have yesterday data in it from morning to afternoon. Before inserting current data in it all the yesterday's rows will be cleared and table will be start to be updated dynamically from noon till evening .But I need to run the query running from morning onwards and my query should not fetch yesterday's data. So from morning to afternoon I should ideally wait for data to come and should not fetch any rows and start to fetch once the current date data was inserted into it.
I need to run the query for every five minutes and when I run the query I should get all the latest rows in the table so that whenever there is a update in the table those rows will be fetched .
For example when the table is updated from 1 PM onwards. I should get all the rows when i start the first query and after five minutes when i once again run the same query at 1.05 i should get all the rows inserted between 1PM and 1.05PM.
My idea :
Select max(time) from emp;
At the start of the day I should check the max time in the table and it will be definitely yesterday date so I will set today date(2018-07-14 00:00:00) in a local variable or if it's today's date then that value will be stored in the local variable.
I can also do the same in the above query by comparing it with sysdate like below query but not sure about the performance as I saw it took time by comparing with all the rows I guess. See the modified above query below
select max(time) from emp where time = sysdate;
After getting the max time from the table , will have it one variable say lastquerytime, then query the table which has rows greater than this time stamp so that we can fetch all the latest rows for every five minutes.
Select id,time,value from emp where time > lastquerytime;
So the idea is getting all the rows and check the maximum timestamp in it and query the table next time with rows having timestamp greater than this max timestamp. Like this need to do the same for every five minutes till the end of the day.
Now I am using two queries to achieve this scenario.
Any suggestions for better approach and queries to write for this scenario will help me a lot.
You should use something like to get data from yesterday:
SELECT id,time,value FROM emp WHERE time BETWEEN TRUNC(SYSDATE - 1) AND TRUNC(SYSDATE) - 1/86400
If I understand correctly, you want all rows on the maximum date of the table.
If so:
select e.*
from emp
where time >= (select trunc(max(time)) from emp);
If you want the results based on clock-time, then you would use trunc(sysdate) instead.
Using PostgreSQL 9.1 table, I want to update day column with extracting day number from timestamp column.
table:
id|name|timesatmp
-----------------
I created a day column then tried to extract the day number following:
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 16
http://www.postgresql.org/docs/current/static/functions-datetime.html
When i tried to update table using:
UPDATE table SET day =
( SELECT EXTRACT (DAY FROM "timestamp") FROM table);
I got this error:
More than one row returned by a subquery used as an expression
I've found that i can repair my statement by adding LIMIT 1 to the subquery to ensure that at most 1 row is returned. but still not sure if it's the right thing to do.
I don't see the need for this sub-select, just update the column:
UPDATE table SET day = EXTRACT (DAY FROM "timestamp");
The real question is though: why do you want to store information that can be derived from the existing data (and in this case: without any additional cost)
Btw: timestamp is a horribly name for a column. First because it's also the name of a datatype and thus is really confusing. Secondly (and more importantly I think) it doesn't document at all what the column contains. A "start date"? an "end date"? a "due date" a "valid date"? ...