i have thousands of timestamps, is it possible to get MySQL to sort these into a day / count array? Rather then doing it via PHP or JS.
The basic query is basically
$mysqli->query("SELECT datetime FROM `users` WHERE `datetime`");
And i need to present them on to a chart that takes date / count values. Id like to display it in a daily interval.
select date(datetime) as `day`, count(*)
from your_table
group by date(datetime)
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.
My web application has a report that shows the number of logins by a particular user each week. However, I'm struggling to get the query just right. The problem I'm running into is that I can't seem to get the weeks during which the user did not login at all.
Currently my query looks like this:
SELECT
DATE_ADD(logDate, INTERVAL(1-DAYOFWEEK(logDate)) DAY) weekStart,
DATE_ADD(logDate, INTERVAL(7-DAYOFWEEK(logDate)) DAY) weekEnd,
COUNT(*) loginCount
FROM log
WHERE
logDate > $startDate AND
logDate < $endDate
I would create a table for week numbers:
CREATE TEMPORARY TABLE weeks (weeknum INT PRIMARY KEY);
Then populate that table with integer values 0..53.
Then join it to your log table using the WEEK() function:
SELECT weeks.weeknum, COUNT(*) loginCount
FROM weeks LEFT OUTER JOIN log ON weeks.weeknum = WEEK(log.logDate)
WHERE log.logDate BETWEEN ? AND ?
GROUP BY weeks.weeknum;
If you need this query to support a date range that spans multiple years, use YEARWEEK() instead, and populate your temp table with more rows in the YYYYWW format like values returned by YEARWEEK().
I'm not sure if this is even within the scope of MySQL to be honest or if some php is necessary here to parse the data. But if it is... some kind of stored procedure is likely necessary.
I have a table that stores rows with a timestamp and an amount.
My query is dynamic and will be searching based on a user-provided date range. I would like to retrieve the SUM() of the amounts for each day in a table that are between the date range. including a 0 if there are no entries for a given day
Something to the effect of...
SELECT
CASE
WHEN //there are entries present at a given date
THEN SUM(amount)
ELSE 0
END AS amountTotal,
//somehow select the day
FROM thisTableName T
WHERE T.timeStamp BETWEEN '$start' AND '$end'
GROUP BY //however I select the day
This is a two parter...
is there a way to select a section of a returned column? Like some kind of regex within mysql?
Is there a way to return the 0's for dates with no rows?
select * from thisTableName group by date(created_at);
In your case, it would be more like
SELECT id, count(id) as amountTotal
FROM thisTableName
WHERE timeStamp BETWEEN '$start' AND '$end'
GROUP BY DATE(timeStamp);
Your question is a duplicate so far: link.