SQL time format hours minutes in access - ms-access

I want to create a query in Access that will sum up the total of time giving per project.
I have a database that look like:
Time | Project
------------------
1:00 | 1455
2:30 | 1666
0.50 | 1455
0.45 | 1455
I want to know how many hours were given to all project.
So far I have created a query that return the total of hours per project, which is good.
However, the format of the time is odd.For instance a project that should return 17:10 hours return this 0.715277777777778
I am trying to format the field Time so the total return a normal looking format like 17:10 hours instead of all those decimals.
This is the SQL I have right now
SELECT Sum(Dan.Time) AS SumOfTime, Dan.Project
FROM Dan
GROUP BY Dan.Project
ORDER BY Dan.Project;
Can anyone help?

You can use format function:
Format(Sum(Dan.Time),"hh:nn") AS SumOfTime
P.S. DateTime in ms Access are in fact double where 1 is one day, and 1/24 is one hour and so on.

I'm not working with ms access, but maybe this helps you.
All times should be saved in the same type. Maybe you should format the times before storing them in the database.
If it is not possible, you can try this
SELECT
Int(Sum(Dan.Time) * 24) & ":" & (Int((Sum(Dan.Time) * 24 - Int(Sum(Dan.Time) * 24)) * 60) + 1)
AS TimeStr,
Dan.Project
FROM
Dan
GROUP BY
Dan.Project
ORDER BY
Dan.Project;
It computs the hours and the minutes from the decimal number and concatenates the numbers as HH:MM.
Maybe there is also a better build in way in ms access.

Related

How to add extra 5 minutes in mysql using query

I'm new for mysql, Already value in time field, I want to update extra 5 minutes in time field using query. I tried so many things but not working.
Here my query:
UPDATE STUDENT SET START_TIME = ADDTIME(START_TIME, 500) WHERE ID = 1;
Above query working but one issue is there that is, If my field having 23:55:00.
I want result after executing query 00:00:00 but it updates 24:00:00.
Anyone help me!
Thanks in advance!!
This is bit tricky, because you only have the time, and you want it to wrap around to 0 after hitting 24 hours. My approach is to extract the number of seconds from START_DATE, add 5 minutes, then take the mod of this by 24 hours to wrap around to zero if it exceeds one day's worth of seconds.
UPDATE STUDENT
SET START_TIME = CAST(STR_TO_DATE(CAST(MOD((TIME_TO_SEC(START_TIME) + 300), 86400) AS CHAR(5)), '%s') AS TIME)
WHERE ID = 1
In the demo below, you can see the logic in action which correctly converts 23:55:00 with five minutes added to become 00:00:00.
SQLFiddle
However, the easiest solution in your case might be to just use a DATETIME and ignore the date component. Then the time should wrap automatically to a new day.
select addtime('23:55:00', '00:06:00');
output - 24:01:00 (Ideally it is right, because time datatype represents only time, if it converts to 00:01:00 then time component looses 24hr, which is wrong)
select addtime('2016-09-01 23:55:00', '00:06:00');
output - 2016-09-02 00:01:00 (In this case, 24hr gets added in date so time component is represented as 00:01:00)
If the requirement is to get it as 00:01:00 then here is the workaround -
SELECT TIME((ADDTIME(TIME('23:59:59'), TIME('02:00:00')))%(TIME('24:00:00')));
reference -
ADDTIME() return 24 hour time

Access Report Subgroup summing

In MS Access 2010, assume a query contains the following results:
Date Activity Hours
1.9. Reading 1
1.9. Writing 2
2.9. Reading 1
3.9. Talking 1
4.9. Reading 3
1.10. Talking 2
1.10. Writing 1
2.10. Reading 2
3.10. Talking 2
4.10. Reading 1
the Report should show the sum of hours spent each month grouped by activity, something like
Month Activity Hours
September Reading 5
Writing 2
Talking 1
October Reading 3
Writing 1
Talking 4
using the wizard I mange to get a report which looks like
Month Activity Hours
September Reading 1
Reading 1
Reading 3
Writing 2
Talking 1
October Reading 2
Reading 1
Writing 1
Talking 2
Talking 2
which is nearly what I'd like to have but.. ?
I tried =sum(Hours), but then all Hours fields simply contain the total sum of all hours, and I still get several lines for the same activity.
Actually the report is more complicated. In a form prior to the report, the user can enter a date range (e.g. September 1 - October 30). The report should sum over the whole time range and not break up to months, that is:
Date range Activity Hours
9/1 - 10/30 Reading 8
Writing 3
Talking 5
Create a query with a SQL like this:
SELECT MonthName(Month([Date])), Activity, SUM(Hours)
FROM yourTable
WHERE [Date] >= xxx AND [Date] <= yyy
GROUP BY MonthName(Month([Date])), Activity
The key element is the GROUP BY clause, it sums the Hours per Month + Activity.
Then base your report on that query.
BTW, it is not a good idea to call a column "Date", it's a reserved word in Access.

Extracting datapoint from mySQL

I have a table within MySQL which is being used to store user session information across a fleet of instances. The structure of the table is pretty simple, with 6 fields.
Item: 37824
Timestamp: 2014-10-30 23:01:41
Active: 14
Idle: 2
Total: 16
Server:
The metrics are collected every 5 minutes.
I have a query which pulls the SUM of one of the metrics for the past 5 minutes.
select SUM(metric_activeSessions) from metrics_tbl WHERE metric_timeStamp > NOW() - INTERVAL 5 MINUTE
I want to build a chart from these metrics, using one of the many great libraries available, but yet to be determined.
I would like the chart to show the SUM of a given metric every over a period of 60 minutes, at 5 minute intervals.
- 09:00 : 12
- 09:05 : 15
- 09:10 : 18
- 09:15 : 21
- 09:20 : 28
I have no idea how to write an SQL query to collect information in that way and was hoping someone out there may be able to help me out.
Thank you in advance for any ideas,
Cheers,
Mitch
Assuming metric_timeStamp is formatted as a mysql timestamp, try something like this..
SELECT SUM(metric_activeSessions), MAX(metric_timeStamp) as max_time, MIN(metric_timeStamp) as min_time,
FROM metrics_tbl
WHERE metric_timeStamp > NOW() - INTERVAL 60 MINUTE
GROUP BY unix_timestamp(metric_timeStamp) div 300
Use that as a starting point. I'm eating ice cream right now so I can't run this myself, but that's the general idea.
Edit: fixed syntax issue
Edit 2: I was grouping by 10 minutes. Changed it to 5 minutes
Edit 3: If your rows contain a running total, then change
SUM(metric_activeSessions)
to
MAX(metric_activeSessions)
in the query above.

Whats the best way to store a time duration in a MySQL larger than the TIME range?

I'm in need of a method to store a time duration in a db field. I'm building a website where customers should be able to choose how long they would like an advert to display from a particular start date.
I had thought about using TIME but that has a max of '838:59:59' which works out at about 34 days. Its possible that a client would want an advert to exist for longer than that.
So what would be the best way to deal with this? Just a really large INT?
If you intend to have a column for start time and one for duration, I think you can store it in seconds. So, I assume you will have something like this;
+-----------+--------------------------+------------------+
| advert_id | start_time | duration_seconds |
+-----------+--------------------------+------------------+
| 2342342 |'2012-11-12 10:23:03' | 86400 |
+-----------+--------------------------+------------------+
(For the sake of the example, we will call this table adverts)
advert_id - a key pointing to your advert
start_time - the time the advert should start (data type - TIMESTAMP)
duration_seconds - Time in seconds that the advert is supposed to "live" (INTEGER(11)
SELECT TIME_TO_SEC(timediff(now(),start_time)) as 'time_difference_in_seconds_since_advert_started' FROM adverts;
If you want to get only adverts that have not expired, you will run a query like this;
SELECT * FROM `adverts` WHERE TIME_TO_SEC(timediff(now(),start_time))<=`duration_seconds`;
That's one way I would do it if I were to go with the "duration" field.
Yes, you can store time as INT data type (or another big integer: MEDIUMINT, LONGINT). Then use you can easily get days and time part from this, e.g. -
SELECT time DIV 86400 AS days, SEC_TO_TIME(column1 MOD 86400) AS time FROM table
Where 86400 is a number of seconds in 24h (60 * 60 * 24 = 86400).
not the best solution but you can add one column in your db, and check when time is more than 24 hours, calculate it as 1 day and write in that column, and all the rest time write in time column. But selecting from db you should calculate also that column of days

Returning rows of aggregate results from single SQL query

I have a MySQL table containing a column to store time and another to store a value associated with that time.
time | value
------------
1 | 0.5
3 | 1.0
4 | 1.5
.... | .....
The events are not periodic, i.e., the time values do not increment by fix interval.
As there are large number of rows (> 100000), for the purpose of showing the values in a graph I would like to be able to aggregate (mean) the values for an interval of fixed size over the entire length of time for which the data is available. So basically the output should consist of pairs of interval and mean values.
Currently, I am splitting the total time interval into fixed chunks of time, executing individual aggregate queries for that interval and collecting the results in application code (Java). Is there a way to do all of these steps in SQL. Also, I am currently using MySQL but am open to other databases that might support an efficient solution.
SELECT FLOOR(time / x) AS Inter, AVG(value) AS Mean
FROM `table`
GROUP BY Inter;
Where x is your interval of fixed size.
I've usually solved this through a "period" table, with all the valid times in it, and an association with the period on which I report.
For instance:
time day week month year
1 1 1 1 2001
2 1 1 1 2001
....
999 7 52 12 2010
You can then join your time to the "period" table time, and use AVG.