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"
Related
I have rows of user data. I store the createDate, which is the timestamp in milliseconds when the user registered. I want to get the total number of registrations per month. When I try to do that, I don't get any rows returned. Here's the query I'm using
SELECT COUNT(*) FROM users WHERE YEAR(createDate) = 2023 GROUP BY MONTH(createDate)
createDate is BIGINT and is the date in milliseconds
I guess your createDate column is defined as TIMESTAMP(3), to get millisecond resolution. LAST_DAY() comes in handy here.
Try this:
SELECT COUNT(*), LAST_DAY(createDate) month_ending
FROM users
WHERE createDate >= '2023-01-01'
AND createDate < '2024-01-01'
GROUP BY LAST_DAY(createDate)
The date range test I use for getting the dates in a single year is sargable. That is, it can be accelerated by an index on createDate, where YEAR(createDate) cannot be.
This approach generates a useful result set if you run it on a multi-year date range.
But, if your result set is empty (has no rows), the result set from this query will be too. That might mean:
your table has no dates in that range, or
your createDate data type is something other than TIMESTAMP or DATETIME. (You didn't show us the table definition.)
It sounds like you need to convert to/from unix time:
SELECT COUNT(*), LAST_DAY(FROM_UNIXTIME(createDate/1000)) month_ending
FROM users
WHERE createDate >= UNIX_TIMESTAMP('2023-01-01') * 1000
AND createDate < UNIX_TIMESTAMP('2024-01-01') * 1000
GROUP BY month_ending
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 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 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'
As the title says, the row count differs when doing a select using DATE and DATETIME. Please advise.
I'm trying to select rows between 1st and 5th Jan, 2012. The date column datatype is bigint (UNIX timestamp).
select * from table_name
where sample_timestamp between unix_timestamp('2012-01-01')*1000 and unix_timestamp('2012-01-05')*1000
If I include the time in HH:MM:SS, the rows returned are correct i.e.
select * from table_name
where sample_timestamp between unix_timestamp('2012-01-01 00:00:00')*1000 and unix_timestamp('2012-01-05 23:59:59')*1000
Any input will be much appreciated. Thanks.
'2012-01-05' is actually '2012-01-05 00:00:00' which is not what you're writing in the second select.
I suspect what you mean to do is
select * from table_name
where sample_timestamp >= unix_timestamp('2012-01-01')*1000
and sample_timestamp < unix_timestamp('2012-01-06')*1000
which as a bonus handles leap seconds correctly too :)