Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What I want to do is simple
I have a df full of start_times & finish_times.
All start & finish in the same day except 2 rows
The time is in a 24 hr format
I want to calculate the min, max, & avg of the calculated time duration.
The problem is out of the 230 rows, 2 of the elapsed times reset.
e.g. One Start time is 2020-10-14 23:37:26 & its corresponding finish time is 2020-10-15 00:19:47
and I have another row where the exam starts 1 day & finishes the next
I stored the values as DATETIME
How do I go about converting this to my desired output?
SELECT AVG(SEC_TO_TIME(TIMESTAMPDIFF(MINUTE, Start_Time, Finish_Time))) AS
Avg_Exam_Duration,
MIN(SEC_TO_TIME(TIMESTAMPDIFF(MINUTE, Start_Time, Finish_Time))) AS
Min_Exam_Duration,
MAX(SEC_TO_TIME(TIMESTAMPDIFF(MINUTE, Start_Time, Finish_Time))) AS
Max_Exam_Duration,
Room_ID FROM exam
GROUP BY Room_ID;
Desired output would correctly calculate the Max, Min, & Avg of the duration (time elapsed) between time in minutes
Current Output
Instead of:
TIMEDIFF(Finish_Time, Start_Time)
use:
SEC_TO_TIME(TIMESTAMPDIFF(SECOND, Start_Time, Finish_Time))
(Note the changed order of parameters.)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a daily stock transaction table
T1(symbol, transDate, closingPrice, PrevQtrChange).
The last column is empty. I need an update statement that, for a given symbol, will get the closing price from the previous quarters transaction. Because of weekends, holidays, etc, i can't do a self join on the date being date-90 days. I could do it with a cursor, but ugh. And, the table contains millions of rows, so a cursor would be extremely slow, even with an index.
I'm a C/C++ programmer so while I know some SQL, doing this efficiently is something I'm unsure of.
Thanks in advance.
You can use window functions. The idea for the previous price is:
select t.*,
last_value(closingPrice) over
(partition by symbol
order by transDate
range between unbounded preceding and interval 90 day preceding
) as prev_quarterprice
from t;
You can then incorporate this into an update:
update t join
(select t.*,
last_value(closingPrice) over
(partition by symbol
order by transDate
range between unbounded preceding and interval 90 day preceding
) as prev_quarterprice
from t
) tt
on tt.symbol = t.symbol and tt.transDate = t.transDate
set t.PrevQtrChange = closingprice - tt.prev_quarterprice
where tt.PrevQtrChange is null ;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to run a query in sequelize or SQL where select current month data and only show just upcoming date data like some kind of events if that day pass then next event or date will be showing like this I have 3 dates in this month
1 2020-03-15
2 2020-03-22
3 2020-03-27
So before 15 I want to only no 1 date after 15march pass I want only no 2 date and goes on
If you want to show the next date in the future, then it would be something like:
select t.*
from t
where date > current_date
order by date asc
fetch first 1 row only;
The exact syntax might vary by database -- say now() or getdate() instead of current_date; or select top (1) or limit 1 instead of the fetch clause.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Fiddle link: https://www.db-fiddle.com/f/eJTJnftZvbDsnoYT2RNSVJ/0
I need to add specific number of days to the current date, then return 50% date.
How can I add the days to today's date and return 50% of the date?
So the first row output from DB Fiddle link would be: 2020-03-24 because from today's date plus 100 = 2020-05-13 and 50% of 100 is 2020-03-24
Visual output:
days todayDate 50%
100 2020/02/03 2020-03-24
How can this be achieved on MySQL?
Why not just add half the number of days?
select curdate() + interval (100 * 0.5) day
-----------------------------^ number of days
-----------------------------------^ 50%
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
-this is the table of picture i want to retrieve datas from
I need two queries.
the first one is to get the actionNo of the latest stopTime before the current time.
the second query is to get the actionNo if the latest actionTime before the current time.
Example:
If the current time was: 00:46:20
the first query is to get actionNo 9 (stoptime: 00:45:00)
the second query is to get actionNo 8 (actiontime: 00:41:30)
-this is eg of range to get data picture
SELECT MAX(id) FROM timestuff WHERE `actionTime` < CURTIME();
SELECT MAX(id) FROM timestuff WHERE `stopTime` < CURTIME();
This gets you the latest id of the action/stoptime before the current time, will work if your table always is in the order as you have shown (that means, ids display the chronology of your time values - which isn't the case in your data as I just noticed).
For your case: you can take another step and add a inner join on the time closest to the current time and then look up the id of that data.
First query: For the latest stopTime before the current time:
SELECT
timestuff.id
FROM
(
SELECT
MAX(stopTime) AS maxTime
FROM
timestuff
WHERE
`stopTime` < CURTIME()
) AS time
INNER JOIN timestuff ON timestuff.`stopTime` = time.maxTime;
Second query: For the first actionTime after the current time:
SELECT
timestuff.id
FROM
(
SELECT
MIN(actionTime) AS minTime
FROM
timestuff
WHERE
`actionTime` > CURTIME()
) AS time
INNER JOIN timestuff ON timestuff.`actionTime` = time.minTime;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
So I have a table of entries with a startdatetime and enddatetime, I want to be able to see overlap like employee coverage. For instance I have 10 employees on shift with different start times and end times and I want to be able to query by hour and see how many employees are available per hour.
I had not tried because I did not find a place to start, I do something similar when they is incoming tasks at a specific time to see how many in each hour, but I am trying to see coverage in each hour of the day based on when an employee starts and ends, like 8 hour shift, 12 hours shift, etc.. So That I can see that I have 2, 3, 4, etc... coverage and map to number of tasks that come in and are planned. Something below looks promising but its only for 1 time for each query, would like a query that can bring back each hour and how many cover in each hour.
Any help would be great.
Try something like this:
SELECT count(*) as TOTAL
FROM table
WHERE '2015-05-28 12:00:00'
BETWEEN startTime and endTime
Obviously you need to change this '2015-05-28 12:00:00' with the desidered date/time you're looking for.
Add others condition if needed...
EDIT
Adding the extra request:
Grouping by date and hour gives you the total count of coverage in a single hour per day.
SELECT date(date_start) as DAY, hour(date_start) as HOUR, count(*) as TOTAL
FROM table
group by date(date_start), hour(date_start)
You can abviously add one or more condition.
ie. in this example below you'll get the total rows per hour in a single day (specified)
SELECT date(date_start) as DAY, hour(date_start) as HOUR, count(*) as TOTAL
FROM table
WHERE date('2015-05-28') = date(startTime)
group by date(date_start), hour(date_start)