473,809 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.M yColumn) as '15:00-16:00' from MyDB.MyTable WHERE
hour(MyTable.My Column) like '%15%' AND MyTable.MyColum n BETWEEN
'2005-05-01 00:00:00' AND '2005-05-31 23:59:59'

Any help?

Jul 23 '05 #1
6 6904
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.M yColumn) as '15:00-16:00' from MyDB.MyTable WHERE
hour(MyTable.My Column) like '%15%' AND MyTable.MyColum n 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.My Column) AS Hour, COUNT(MyTable.M yColumn) AS Count
FROM MyDB.MyTable
GROUP BY HOUR(MyTable.My Column);

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.MyColum n) AS Count
FROM hours LEFT OUTER JOIN MyTable ON hours.hr = HOUR(MyTable.My Column)
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.MyColum n) AS Count
FROM hours LEFT OUTER JOIN MyTable
ON hours.hr = HOUR(MyTable.My Column) AND
MyTable.MyColum n 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
2515
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 TABLE ...") ) . When I look into the table directly (with phpmyadmin) the value has been correctly changed. But when I use again mysql_query() to print the new value,
3
5012
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 work out how to get the format right so the %s is subsituted with the contents of the variable, I think I just have the quoting wrong, can anyone advise? tablename contains the table I want to use datavalue contains the data I want to use...
3
1824
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 work out how to get the format right so the %s is subsituted with the contents of the variable, I think I just have the quoting wrong, can anyone advise? tablename contains the table I want to use datavalue contains the data I want to use...
0
1143
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' There is one record for each users' pick. 0 points are given for a loss and 1-4 points are given for a win. I've already created a query that gives me the top players by overall points but I'd like to expand that to include the number of...
0
775
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) http://www.oursite.com/OurApp/OurApp.exe?somequerystring (this doesn't) both worked fine in framework 1.1. Is this a known issue? We pass in
2
8493
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 $$ DROP PROCEDURE IF EXISTS `mailmsgs`.`sp_AddNew` $$ CREATE PROCEDURE `mailmsgs`.`sp_AddNew` (date_time datetime, sender char(255),
1
2527
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 then don't. I need to check setgroup. If the one in the row matches the one of the insert then do the update otherwise don't. Here is my code for the insert update.
1
3773
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 addString (const std::string s); void print_strings () const;
2
3276
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 Visual C# with .net CF 3.5. Target platform is Windows Mobile. Using Windows Mobile 6 SDK. Example code is below passing only one parameter: private void RefreshData() { string temp_url="file://\\Program...
0
9602
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.