473,378 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

please help with mysql query string

I need to create report where will be shown total quantity of all rows
for every hour in some data range.

I have a table where I have column in DATETIME format. I have problem
to create sql query where I have to get result of the all counts for
every hour of the date range.
Something like that:
from starting hour to final hour = total row count (if there is an
entries),
from 01:00 to 02:00 = 5,
from 02:00 to 03:00 = 0,
.........
from 23:00 to 00:00 = 8, between 2005-01-01 and 2005-01-31.

I can get result for one hour but how to get results for all hours? I
am newbie in SQL....

SELECT count(MyTable.MyColumn) as '15:00-16:00' from MyDB.MyTable WHERE
hour(MyTable.MyColumn) like '%15%' AND MyTable.MyColumn BETWEEN
'2005-05-01 00:00:00' AND '2005-05-31 23:59:59'

Any help?

Jul 23 '05 #1
6 6888
Ljoha wrote:
I can get result for one hour but how to get results for all hours? I
am newbie in SQL....

SELECT count(MyTable.MyColumn) as '15:00-16:00' from MyDB.MyTable WHERE
hour(MyTable.MyColumn) like '%15%' AND MyTable.MyColumn BETWEEN
'2005-05-01 00:00:00' AND '2005-05-31 23:59:59'


The best way to do what you want in SQL is to use the GROUP BY clause.

SELECT HOUR(MyTable.MyColumn) AS Hour, COUNT(MyTable.MyColumn) AS Count
FROM MyDB.MyTable
GROUP BY HOUR(MyTable.MyColumn);

Regards,
Bill K.
Jul 23 '05 #2
Bill, thank you for your replay!

With this query, if my table has no row for example for hour 13:00
-14:00, I can't get result. In this query I will get only that
hours that has rows for certain time period. Is it possible to get all
24 hours range and if some hour doesn't have a value it will be with
zero in result?

Jul 23 '05 #3
Ljoha wrote:
Bill, thank you for your replay!

With this query, if my table has no row for example for hour 13:00
-14:00, I can't get result. In this query I will get only that
hours that has rows for certain time period. Is it possible to get all
24 hours range and if some hour doesn't have a value it will be with
zero in result?


It's tricky to get GROUP BY to create groups for values that do not
occur in your table. To do this, you need to have a list available of
all the values for the group that you need to report.

For instance, create a temporary table containing the values of all hours:

CREATE TEMPORARY TABLE hours (hr INTEGER);
INSERT INTO hours VALUES
(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
(10), (11), (12), (13), (14), (15), (16), (17), (18), (19),
(20), (21), (22), (23);

Now join that hours table to your records in MyTable using a LEFT OUTER
JOIN, so you'll get the hour number even if there are no matching records.

SELECT hours.hr, COUNT(DISTINCT MyTable.MyColumn) AS Count
FROM hours LEFT OUTER JOIN MyTable ON hours.hr = HOUR(MyTable.MyColumn)
GROUP BY hours.hr;

The COUNT(DISTINCT expr) gives the number of distinct non-null values in
the expression. So if there are only NULLs (that is, no rows in MyTable
match hour.hr) the result should be zero.

Read more about COUNT at:
http://dev.mysql.com/doc/mysql/en/gr...functions.html

Read more about temporary tables at:
http://dev.mysql.com/doc/mysql/en/create-table.html

Read more about using LEFT OUTER JOIN in a SQL book of your choice.

Regards,
Bill K.
Jul 23 '05 #4
Thanks, working great! But unfortunately I need this query only if I
can limit this query with date range. I tried to put in your query
"BETWEEN '2005-06-01 00:00:00' AND '2005-06-07 23:59:59' " - but it
doesn't work... :-(
this is for analysing - one week with another, or one month with
another....

Jul 23 '05 #5
Ljoha wrote:
Thanks, working great! But unfortunately I need this query only if I
can limit this query with date range. I tried to put in your query
"BETWEEN '2005-06-01 00:00:00' AND '2005-06-07 23:59:59' " - but it
doesn't work... :-(


Probably depends where you put it! :-)

When using outer joins, and making restrictions on the side of the join
that might be unmatched, add the conditions to the join condition,
instead of in a WHERE clause.

SELECT hours.hr, COUNT(DISTINCT MyTable.MyColumn) AS Count
FROM hours LEFT OUTER JOIN MyTable
ON hours.hr = HOUR(MyTable.MyColumn) AND
MyTable.MyColumn BETWEEN '2005-06-01 00:00:00' AND '2005-06-07
23:59:59'
GROUP BY hours.hr;

If you were to put the BETWEEN condition it in a WHERE clause, the hours
for which the right side of the join resulted in NULL don't match the
BETWEEN condition, so those rows are eliminated from the result set, and
you'll have no entries to count for certain hours. If you put the
condition in the join clause, you limit the possible records from
MyTable that might match, without eliminating the hours.

Regards,
Bill K.
Jul 23 '05 #6
Bill, thank you soooo much! You are great profi!
I have to study a little with sql query :-)

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JB | last post by:
Hello, I'm experiencing a very strange problem with a MySQL query. Firstly, I use mysql_query() to print the value that I need to modify I modify it by doing an update ( mysql_query("UPDATE...
3
by: RiGGa | last post by:
Hi, I am trung to create a mysql query string that contais two variables, the first holds a table name and the second holds the values as a tuple. I have tried the following however I can not...
3
by: RiGGa | last post by:
Hi, I am trung to create a mysql query string that contais two variables, the first holds a table name and the second holds the values as a tuple.  I have tried the following however I can not...
0
by: Sundin | last post by:
Hi I'm running a sports pool and I'm trying to display stats from the results of the picks. The table is 'results' and the fields are 'pick_number', 'game_number', 'user_id', 'points' and 'date'...
0
by: D Witherspoon | last post by:
We have several no touch deployment applications. We get an error if we have a ? after the exe name For example: http://www.oursite.com/OurApp/OurApp.exe (this works) ...
2
by: Don | last post by:
I'm new to MySQL and am having a frustrating time figuring out some of the tools. I'm getting ERROR 1044 when I try to execute the following sp. ------------------------------ DELIMITER $$ ...
1
by: admin | last post by:
I have this query which works to insert and update the row. Problem is I need it to check and see if a value in the row is the same as the one being set if it is then do the update part if not...
1
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
2
by: mtnbiker | last post by:
Am a novice at this. Trying to write a piece of code for personal use. Intent is to load a web page from a file (.html) and pass to it three parameters using a query string. Code is written in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.