I have to query a calls.csv table (below) to extract the total calls, total talk seconds and average call seconds:
By day
For the queue 24710 during the month of August 2015
Callid Qid Tm_init Tm_disc
780164900 24710 1422835548 1422835559
780164901 24710 1422835562 1422835687
I arrived at the following query, however, I have been having some difficulties to write the query regarding 'by day' and 'queue 24710 during the month of August 2015':
SELECT
(AVG(`Tm_disc` - `Tm_init`)) AS AVARAGE_CALLS_SECONDS,
(SUM(`Tm_disc` - `Tm_init`)) AS TOTAL_SECONDS_CALLS,
COUNT(`Callid`) AS TOTAL_NUMBER_CALLS
FROM
calls_database.`calls`
Any help, please?
Thank you
You can use FROM_UNIXTIME() to convert unix date to timestamp and then regular datetime functions such as DATE() functions to extract day from the unixtime.
SELECT
(AVG(`Tm_disc` - `Tm_init`)) AS AVARAGE_CALLS_SECONDS,
(SUM(`Tm_disc` - `Tm_init`)) AS TOTAL_SECONDS_CALLS,
COUNT(`Callid`) AS TOTAL_NUMBER_CALLS
FROM
calls
WHERE
Qid=24710
AND
MONTH(FROM_UNIXTIME(Tm_init))=2
AND
YEAR(FROM_UNIXTIME(Tm_init))=2015
GROUP BY DATE(FROM_UNIXTIME(Tm_init))
SQLFiddle: http://sqlfiddle.com/#!9/58dfcc8/9
Then add your filters such as Qid=1234.
Related
Hi I'm using the MySQL query below along side Grafana, and data relating to my electricity usage. The data collected ranges from late 2021 to current date, however the result only ranges from December 21 to November 22.
SELECT
Time AS "time",
SUM(Rate/800) as kWh
FROM Log
GROUP BY MONTH(STR_TO_DATE(time, "%Y-%m-%d"))
Can someone kindly assist with amending the query to display all relevant months from 2021 to 2023 and so on please.
Please add your table structure and some sample data to your question.
Your current query is non-deterministic, and if you had ONLY_FULL_GROUP_BY enabled, would be failing with a 1055 error. You can read more about MySQL Handling of GROUP BY here.
You are grouping by MONTH(STR_TO_DATE(time, "%Y-%m-%d")) which will return 1 for 2021-01-02, 2022-01-03 and 2023-01-04. You need to group by both the year and month.
If you run this modified version of your query you should be able to see what is happening:
SELECT
MONTH(STR_TO_DATE(`Time`, '%Y-%m-%d')) AS `m`,
MIN(`Time`) AS `min_time`,
MAX(`Time`) AS `max_time`,
SUM(`Rate`/800) AS `kWh`
FROM `Log`
GROUP BY MONTH(STR_TO_DATE(`Time`, '%Y-%m-%d'));
Your use of STR_TO_DATE() appears unnecessary as you are using the standard date format %Y-%m-%d.
SELECT
DATE_FORMAT(`Time`, '%Y-%m-01 00:00:00') AS `time`,
SUM(`Rate`/800) AS `kWh`
FROM `Log`
GROUP BY `time`
With the date format '%Y-%m', 2021-01-02, 2022-01-03 and 2023-01-04 will be returned as 2021-01, 2022-01 and 2023-01 respectively, and grouped in the correct month.
Try this :
SELECT
Concat(YEAR(STR_TO_DATE(Time, "%Y-%m-%d")),'-', MONTH(STR_TO_DATE(Time, "%Y-%m-%d"))) as month,
SUM(Rate/800) as kWh
FROM Log
GROUP BY YEAR(STR_TO_DATE(Time, "%Y-%m-%d")), MONTH(STR_TO_DATE(Time, "%Y-%m-%d"))
You can use this query to get your desired output :
SELECT
DATE_FORMAT(STR_TO_DATE(Time, "%Y-%m-%d"),"%Y-%m") AS "Month",
SUM(Rate / 800) as kWh
FROM
Log
GROUP BY
DATE_FORMAT(STR_TO_DATE(Time, "%Y-%m-%d"),"%Y-%m"), Time
ORDER BY
STR_TO_DATE(Time, "%Y-%m-%d");
Results looks like :
Month
kWh
2022-01
1700773808
2022-02
1770840497
2022-04
1741656455
2022-05
1738120832
2023-01
1752779938
I have timestamp values in my db. It has values like 2014-11-25 10:30:00.
I need to get all records between two dates and that has time between certain range like between 2014-10-20 to 2014-11-25 and between 9am to 7pm..
I need the query for this...
You can use the following query , I used it in my code for displaying data between two dates.
SELECT * from tablename WHERE columnname BETWEEN '2014-10-20 00:00:00' AND '2014-11-25 23:59:59'
The query includes start time of the particular date to end time of ending particular date.
You edit your query according to your start and end timings.
You can use internal mysql functions for convert datetype.
I think you need DATE() and TIME() functions.
Details you can find here
Thanks for your reply guys. I have found the answer
SELECT * FROM alerts
WHERE DATE BETWEEN '2014-11-16' AND '2014-11-26'
AND TIME(DATE) BETWEEN '09:00' AND '19:00'
Is giving the expected result.. :-)
I am looking to pull scheduled hours in a given time period. Our start and end schedule times are datetimes so I converted them to timestamps. When I dont sum them everything looks correct, but when I sum them over a time period, the output isnt in a timestamp format and the numbers are incorrect.
The query I am using:
select sal.public_name, sum(timediff(timestamp(end_time), timestamp(start_time)))
from bi.support_agents_list sal
join bi.support_sp_shifts_scheduled ss
on ss.agent_sp_id = sal.sp_id
join bi.support_sp_shifts s
on s.pk_id = ss.pk_id
where date(start_time) between '2014-01-29' and '2014-01-31'
group by sal.public_name
A few examples of results I am getting:
Agent 1: 53000 - when it should be 5.5 hours or 5:30
agent 2: 196000 - when it should be 20 hours
Any thoughts on this? I would prefer my output to be in an hour count so 5 hours and 30 min is formatted as 5.5 rather than 5:30.
try this instead of the sum
date_format(timediff(timestamp(end_time), timestamp(start_time)),
'%k hours, %i minutes, %s seconds') as thesum
like that
select sal.public_name,
date_format(timediff(timestamp(end_time), timestamp(start_time)), '%k hours, %i minutes, %s seconds') as thesum
from bi.support_agents_list sal
When doing aggregate calculations with datetime sum(datetime), the result is not what you expect (=cannot sum datetimes). You will be better off converting the datetime to seconds before the aggregate function and then convert it back to time.
Your aggregate function call would then look something like:
select sec_to_time(sum(unix_timestamp(end_time)-unix_timestamp(start_time)))
Be aware that you may reach maximum value that time datatype can contain and that unix_timestamp starts from 1970.
I have a mysql database that looks like this:
id | userid | timestamp | activity
Timestamp is a datetime data type, I need to get the data grouped by month, day and hour. I am using mysql and php for my scripts.
I am able to do it by month and day with the following query:
$query = "SELECT COUNT(id) as totals FROM security_transactions WHERE YEAR(timestamp) = 2012 GROUP BY MONTH(timestamp), DAY(timestamp)";
I need to do it by month day and hours.
Please help.
Thanks.
You can add , HOUR(TIME(timestamp)) to your group by query providing your column is of DATETIME format
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_hour
Also, from the error messages put in the comments below, it looks like #Aprentice is not using mysql, but I've improved this answer for others looking for mysql.
I have never used mssql, so I can't test this but the following might work to group by nearest hour:
GROUP BY dateadd(hour, datediff(hour, 0, timestamp, 0)
Just take it one step further and use HOUR() as well. You will first need to extract the time portion of the timestamp. But guess what, there is a function for that as well ;)
If I have MySQL query like this, summing word frequencies per week:
SELECT
SUM(`city`),
SUM(`officers`),
SUM(`uk`),
SUM(`wednesday`),
DATE_FORMAT(`dateTime`, '%d/%m/%Y')
FROM myTable
WHERE dateTime BETWEEN '2011-09-28 18:00:00' AND '2011-10-29 18:59:00'
GROUP BY WEEK(dateTime)
The results given by MySQL take the first value of column dateTime, in this case 28/09/2011 which happens to be a Saturday.
Is it possible to adjust the query in MySQL to show the date upon which the week commences, even if there is no data available, so that for the above, 2011-09-28 would be replaced with 2011/09/26 instead? That is, the date of the start of the week, being a Monday. Or would it be better to adjust the dates programmatically after the query has run?
The dateTime column is in format 2011/10/02 12:05:00
It is possible to do it in SQL but it would be better to do it in your program code as it would be more efficient and easier. Also, while MySQL accepts your query, it doesn't quite make sense - you have DATE_FORMAT(dateTime, '%d/%m/%Y') in select's field list while you group by WEEK(dateTime). This means that the DB engine has to select random date from current group (week) for each row. Ie consider you have records for 27.09.2011, 28.09.2011 and 29.09.2011 - they all fall onto same week, so in the final resultset only one row is generated for those three records. Now which date out of those three should be picked for the DATE_FORMAT() call? Answer would be somewhat simpler if there is ORDER BY in the query but it still doesn't quite make sense to use fields/expressions in the field list which aren't in GROUP BY or which aren't aggregates. You should really return the week number in the select list (instead of DATE_FORMAT call) and then in your code calculate the start and end dates from it.