I have a database table 'email_log' which would be filled with information as:
user, subject and opened (as datetime)
What I need is a new query from which I can see how many rows contain the column 'opened' between 08:00 and 09:00.
What I had didn't work:
SELECT count(*) as count FROM email_log WHERE opened BETWEEN '00:08:00' AND '00:09:00';
Does anyone know the trick?
You might need to include the entire format for the datetime - since it's got both a date and a time.
See: mysql: get record count between two date-time
try
SELECT count(*) as count FROM email_log WHERE
opened BETWEEN STR_TO_DATE('5/15/2012', '%c/%e/%Y')
AND STR_TO_DATE('5/15/2012', '%c/%e/%Y'); // date only
SELECT count(*) as count FROM email_log WHERE
opened BETWEEN STR_TO_DATE('8:06:26 AM', '%r')
AND STR_TO_DATE('8:06:26 AM', '%r'); // time only
Try this
SELECT COUNT(*) AS COUNT
FROM email_log
WHERE DATE_FORMAT(opened,"%H:%i:%S") > '00:08:00' AND DATE_FORMAT(opened,"%H:%i:%S") < '00:09:00'
Related
Since few days, I am trying to count records per hour from the MySQL database.
I have a table with a lot of records and I have column DATE and column TIME where in DATE I have the date of the record in the format 2022-05-19, and in the column TIME, I have the time of the record in the format 14:59:38.
What I am trying is to count every single day how many records per hour I have. Something like this:
DATE HOUR PCS
22-05-18 06-07 11
22-05-18 08-09 20
......... ..... ..
....... 21-22 33
I have tried many different ways but no success.
For example:
SELECT 'Date', count(*) FROM `root4`
where
DATE between '2022-05-01' and '2022-05-1' AND
TIME BETWEEN '06:11:05' AND '07:11:05'
Any help is highly evaluated.
I would recommend not using reserved words for columns, as you will have to escape them a lot. https://dev.mysql.com/doc/refman/8.0/en/keywords.html
If you stored TIME as a timestamp, you can extract the hour using the HOUR() function and group by that:
SELECT
`DATE`,
HOUR(`TIME`) AS `HOUR`,
COUNT(1)
FROM your_table
GROUP BY
`DATE`,
HOUR(`TIME`)
If you happened to store it as text you can use REGEXP_SUBSTR to get the hour value from your time string.
SELECT
`DATE`,
CAST(REGEXP_SUBSTR(`TIME`, '[0-9]+') AS UNSIGNED) AS `HOUR`,
COUNT(1)
FROM your_table
GROUP BY
`DATE`,
CAST(REGEXP_SUBSTR(`TIME`, '[0-9]+') AS UNSIGNED)
You can format your HOUR column how you want, like displaying 01-02 instead of 1 by using CONCAT, but this is your basic setup.
I have a MYSQL database with the column event_time with timestamps in this format: 2012-07-18 12:54:45.
I need to select any rows that fall into a specified time window e.g. 0700 to 1159. How can I do a SELECT that fetches those rows for any date?
This is what I tried:
SELECT count(id) FROM dataset WHERE event_time >= "07:00" AND <= "11:59"
Here is how to extract time from timestamp:
SELECT count(id)
FROM dataset
WHERE TIME(event_time) BETWEEN "07:00" AND "11:59"
or
SELECT count(id)
FROM dataset
WHERE cast(event_time as time) BETWEEN "07:00" AND "11:59"
Both of them work totally fine for me! Hope my answer help you with your question.
Okay, I figured it out. I can cast the datetime column to TIME, and then do a BETWEEN.
SELECT count(id)
FROM dataset
WHERE TIME(event_time) BETWEEN "07:00"
AND "11:59"
I have a column ifd0_DateTime , in a table named photo, which contains date time in following format: 1966:12:22 17:19:57.
I need to count the number of photos month wise for every year.
So far I have this query. but it doesn't work correctly.
SELECT ifd0_DateTime, count(*) FROM photo
group by YEAR(ifd0_DateTime), MONTH(ifd0_DateTime);
Could anyone please fix this ?
SELECT STR_TO_DATE(LEFT(ifd0_DateTime, 7), '%Y:%m'),
COUNT(*) AS dateCnt
FROM photo
GROUP BY STR_TO_DATE(LEFT(ifd0_DateTime, 7), '%Y:%m')
Instead of date select the Year and Month
Try this
SELECT YEAR(ifd0_DateTime), MONTH(ifd0_DateTime), count(*)
FROM photo
group by YEAR(ifd0_DateTime), MONTH(ifd0_DateTime)
The proper way to store a date time is using the built-in data types. However, it looks like you are storing the value as a string. If so, you just want the first 7 characters:
SELECT LEFT(ifd0_DateTime, 7) as yyyymm, count(*)
FROM photo
GROUP BY LEFT(ifd0_DateTime, 7)
ORDER BY yyyymm;
However, you really should fix the data.
I am using the Graph Reports for the select below. The MySQL database only has the active records in the database, so if no records are in the database from X hours till Y hours that select does not return anything. So in my case, I need that select return Paypal zero values as well even the no activity was in the database. And I do not understand how to use the UNION function or re-create select in order to get the zero values if nothing was recorded in the database in time interval. Could you please help?
select STR_TO_DATE ( DATE_FORMAT(`acctstarttime`,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', count(*) as `Active Paid Accounts`
from radacct_history where `paymentmethod` = 'PayPal'
group by DATE_FORMAT(`#date`,'%y-%m-%d %H')
When I run the select the output is:
Current Output
But I need if there are no values between 2016-07-27 07:00:00 and 2016-07-28 11:00:00, then in every hour it should show zero active accounts Like that:
Needed output with no values every hour
I have created such select below , but it not put to every hour the zero value like i need. showing the big gap between the 12 Sep and 13 Sep anyway, but there should be the zero values every hour
(select STR_TO_DATE ( DATE_FORMAT(acctstarttime,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', count(paymentmethod) as Active Paid Accounts
from radacct_history where paymentmethod <> 'PayPal'
group by DATE_FORMAT(#date,'%y-%m-%d %H'))
union ALL
(select STR_TO_DATE ( DATE_FORMAT(acctstarttime,'%y-%m-%d %H'),'%y-%m-%d %H')
as '#date', 0 as Active Paid Accounts
from radacct_history where paymentmethod <> 'PayPal'
group by DATE_FORMAT(#date,'%y-%m-%d %H')) ;
I guess, you want to return 0 if there is no matching rows in MySQL. Here is an example:
(SELECT Col1,Col2,Col3 FROM ExampleTable WHERE ID='1234')
UNION (SELECT 'Def Val' AS Col1,'none' AS Col2,'' AS Col3) LIMIT 1;
Updated the post: You are trying to retrieve data that aren't present in the table, I guess in reference to the output provided. So in this case, you have to maintain a date table to show the date that aren't in the table. Please refer to this and it's little bit tricky - SQL query that returns all dates not used in a table
You need an artificial table with all necessary time intervals. E.g. if you need daily data create a table and add all day dates e.g. start from 1970 till 2100.
Then you can use the table and LEFT JOIN your radacct_history. So for each desired interval you will have group item (group by should be based on the intervals table.
I need to query a database which will return the number of people subscribed to a particular service during that month.
I use the below query
select subscriberid, count(*) from ABC where updated time like '2013-05-%' ;
In this query I need to update the Updatedtime field to be 2013-06-% when the next month comes and then 07 when the next to next month comes. I want the query to be updated automatically when the next month comes instead of manually changing it every time.
Also note that I want the data for a particular month at a time, so please don't suggest grouping by month as an answer.
One way to do it
SELECT subscriberid, COUNT(*)
FROM ABC
WHERE YEAR(updated_time) = YEAR(CURDATE())
AND MONTH(updated_time) = MONTH(CURDATE())
or
SELECT subscriberid, COUNT(*)
FROM ABC
WHERE updated_time BETWEEN ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)), 1)
AND LAST_DAY(CURDATE())
The following should work fine:
SELECT
subscriberid,
count(*)
from
ABC
where
updatedtime LIKE CONCAT(DATE_FORMAT(NOW(),'%Y-%m'), '-%')
I think you can use DATE_FORMAT function
SELECT subscriberid, count(*) as total
FROM ABC
WHERE DATE_FORMAT(updated_time, "%Y-%m") = "2013-05";
Use the following query:
SELECT subscribersid, updated, COUNT(*) from subscribers
WHERE YEAR(updated) = YEAR(NOW())
AND MONTH(updated) = MONTH(NOW())
You can see it working in this SQL Fiddle
Hope this helps