MySql report grouped by location and hour of day - mysql

I have a mysql table where each entry contains among other, a location string and a datetime field.
I would like to build a mysql query that generates a report with the count(items) grouped by hour of day (on the rows) and the locations (on the columns).
So far I'm manually generating reports grouped by location and I have a WHERE clause where I iterate over each hour of day (24 in number). This is too cumbersome and I would like to do it automatically.
EDIT
This is the query i'm now trying to use based on the answer from fxzuz
SELECT locations.locationName, DATE_FORMAT( '%Y-%m-%d %H', entries.CreatedOn ) , COUNT( pk_entry )
FROM entries
LEFT JOIN locations ON locations.computerName = entries.location
GROUP BY DATE_FORMAT( '%Y-%m-%d %H', entries.CreatedOn )

What's about mysql function DATE_FORMAT?
SELECT location, DATE_FORMAT(datetime, '%Y-%m-%d %H'), COUNT(id) FROM table GROUP BY DATE_FORMAT('%Y-%m-%d %H', datetime);

Related

Group by help ( grouping by multiple, have duplicates)

SO i have a task and i need to group my results by Date and by Provider_name but currently my code is listing out multiple dates and Providers. (need to have one provider per day (25 days in all) so my table shows how many messages the provider got that day and how much did they earn)
This needs to be my result. Result table
But this is what i'm currently getting
This is my code currently
SELECT date_format( time, '%Y-%m-%d' ) AS Date, provider_name, COUNT( message_id ) AS Messages_count, SUM( price ) AS Total_price
FROM mobile_log_messages_sms
INNER JOIN service_instances ON service_instances.service_instance_id = mobile_log_messages_sms.service_instance_id
INNER JOIN mobile_providers ON mobile_providers.network_code = mobile_log_messages_sms.network_code
WHERE time
BETWEEN '2017-02-26 00:00:00'
AND time
AND '2017-03-22 00:00:00'
AND price IS NOT NULL
AND price <> ''
AND service IS NOT NULL
AND service <> ''
AND enabled IS NOT NULL
AND enabled >=1
GROUP BY provider_name, time
ORDER BY time DESC
Can you tell me where i've messed up, i really can't figure out the answer.
Try like this:
....
GROUP BY provider_name, date_format( time, '%Y-%m-%d' )
ORDER BY time DESC
You are grouping time which will group the result by time including hour, minute and second so on ... that is why you getting different count from same day. Try grouping by day instead.
time column is datetime. So its grouped by date and time both rather than just date.
Change GROUP BY statement to
GROUP BY provider_name, date_format( time, '%Y-%m-%d' )

Count on curdate() in mysql

I have a user_entry table which contains a date field. data type is datetime.
data base is mysql.
I want a count of current date and current month and all data of current date.
How can I get this?
I tried below query but it's not working.
select * from table
where DATE(date) = CURDATE()
SELECT date FROM `test` WHERE date = CURDATE()
or
SELECT date FROM `test` WHERE date = DATE('2016-04-04')
it's work.
if you want the number of matches:
SELECT COUNT(date) from test WHERE date = CURDATE()
What is the data type of field 'date'?
To obtain the DAY/MONTH you can use the corresponding functions
SELECT MONTH(date), DAY(date) from test
Moreover, you can use groups to create a complete report
SELECT COUNT(date), date from test GROUP BY DAY(date), MONTH(date)
i used below query and it works for me.
SELECT *
FROM `user_entry`
WHERE DATE( DATE ) = DATE( NOW( ) )

Is there a MySQL Statement for this or are multiple statements needed?

I have a table with MLSNumber, ListingContractDate, CloseDate.
I want to summarize the activity grouped my month starting with the current month and going back to January 2000.
I have this statement which summarizes the ListingContractDate by month.
SELECT COUNT(MLSNumber) AS NewListings, DATE_FORMAT(ListingContractDate,'%M %Y')
FROM Listings
WHERE Neighbourhood = 'Beachside'
AND ListingContractDate >= '2000-01-01'
GROUP BY YEAR(ListingContractDate), MONTH(ListingContractDate)
ORDER BY ListingContractDate DESC
The two problems with this statement are if there is nothing found in a specific month it skips that month, and I would need to return a 0 so no months are missing, and I am not sure how to get the same count on the CloseDate field or if I just have to run a 2nd query and match the two results up by month and year using PHP.
An exceptionally useful item to have is a "tally table" which simply consists on a set of integers. I used a script found HERE to generate such a table.
With that table I can now LEFT JOIN the time related data to it as shown below:
set #startdt := '2000-01-01';
SELECT COUNT(MLSNumber) AS NewListings, DATE_FORMAT(T.Mnth,'%M %Y')
FROM (
select
tally.id
, date_add( #startdt, INTERVAL (tally.id - 1) MONTH ) as Mnth
, date_add( #startdt, INTERVAL tally.id MONTH ) as NextMnth
from tally
where tally.id <= (
select period_diff(date_format(now(), '%Y%m'), date_format(#startdt, '%Y%m')) + 1
)
) t
LEFT JOIN Temp On Temp.ListingContractDate >= T.Mnth and Temp.ListingContractDate < T.NextMnth
GROUP BY YEAR(T.Mnth), MONTH(T.Mnth)
ORDER BY T.Mnth DESC
Logc,
define a stating date
calculate the number of months from that date until now (using
PERIOD_DIFF + 1)
choose that number of records from the tally table
create period start and end dates (tally.Mnth & tally.NextMnth)
LEFT JOIN the actual data to the tally table using
Temp.ListingContractDate >= T.Mnth and Temp.ListingContractDate < T.NextMnth
group and count the data
see this sqlfiddle`

date_trunc PostgreSQL function equal for mySQL

Im trying to retrieve data to make statistics, im using mySQL and i cant get the following function to work - the postgreSQL is working.
I want to retrieve the request for the last month and count the amount of new requests for each day.
postgreSQL
SELECT count(*), date_trunc('day', created_at) as date FROM requests
WHERE(created_at > '2014-08-13 00:00:00') GROUP BY 2 ORDER BY 2 ASC;
*mySQL - my code *
SELECT count(EXTRACT(DAY FROM created_at)), EXTRACT(DAY FROM created_at) as date
FROM `requests`
WHERE EXTRACT(DAY FROM NOW() - INTERVAL 1 MONTH)
GROUP BY date
Final code
SELECT count( * ) , date( created_at ) AS date
FROM `requests`
WHERE DATE( created_at ) > DATE( DATE_SUB( NOW( ) , INTERVAL 1 MONTH ) )
GROUP BY date
The equivalent for your case is date():
select date(created_at), count(*)
from requests
. . .
This isn't a general replacement, but it works to remove the time portion of a date.
EDIT:
Perhaps the better solution for these two databases is:
select cast(created_at as date)
This is ANSI standard and works in both these databases (as well as SQL Server). I personally don't use this in general, lest I accidentally use it in Oracle, causing difficult to find errors. (dates in Oracle have a time component, alas.)

One query to get total # records for each day in a month

Scenario:
I have a table of registrations and there is a datetime column.
I want to display a bar graph on a webpage showing number of registrations per day for the current month.
How can I do that with one query? i.e. get back a resultset with a field for each day (of current month) with the total number of registrations that day
select day(Date), count(*)
from table
where month(Date) = month(NOW()) and year(Date) = year(NOW())
group by day(Date)
"Where" clause selects the current month. You can use count(whatever) or count(distinct whatever) instead of count(*)...
Something like this pseudo-code:
SELECT Day, COUNT(*)
FROM XXX_Users
GROUP BY "date extracted from datetime" as Day
To extract the date from datetime you could use the DATE function:
SELECT Day, COUNT(*)
FROM XXX_Users
GROUP BY DATE(created) as Day
Or you can create a Stored Procedure to enclose all the logic inside