MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 18th, 2013, 09:59 AM
NotionCommotion NotionCommotion is offline
Contributing User
Click here for more information.
 
Join Date: Sep 2006
Posts: 1,464 NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 21 m 9 sec
Reputation Power: 526
GROUP BY date question

Let's say I have the following query. Is there any chance that myDate will fall outside of the given month/year associated with the returned COUNT(*)?

Code:
SELECT COUNT(*) AS total, myDate FROM myTable GROUP BY YEAR(myDate), MONTH(myDate)

Reply With Quote
  #2  
Old January 19th, 2013, 09:36 AM
f_razzoli f_razzoli is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Location: Italy
Posts: 36 f_razzoli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 48 sec
Reputation Power: 1
I'm not sure about what you mean with "fall outside". You don't have any WHERE or HAVING, so all records will be in some group and you'll see all groups.
But I see a problem in the SELECT clause. Not-grouped expressions should not be SELECTed. I think you want something like:

Code:
SELECT COUNT(*), YEAR(myDate), MONTH(myDate) ...


Also, if you have values like NULL or '0000-00-00' in myDate, maybe you want to exclude them with:

Code:
WHERE myDate > 0

Reply With Quote
  #3  
Old January 19th, 2013, 09:58 AM
NotionCommotion NotionCommotion is offline
Contributing User
Click here for more information.
 
Join Date: Sep 2006
Posts: 1,464 NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 21 m 9 sec
Reputation Power: 526
Hello f_razzoli from Italy! I was just there several months ago and had a wonderful time.

Thank you for your reply.

Quote:
Not-grouped expressions should not be SELECTed


Why not? What would happen?

Let's say I run the following query:

Code:
SELECT COUNT(*), myDate, YEAR(myDate), MONTH(myDate)...


Possible returned data will be something like:
Code:
...
213   2012-01-01  2012 01
413   2012-02-03  2012 02
242   2012-03-02  2012 03
...


When I tested it, for each of the returned records, myDate always falls within the given month/year (and appeared to be the first record within the given period).

My concern is that I am going to change indexes or something, and will no longer get this outcome.

I also recognize that it is a little sketchy. The returned myDate is just one of many dates that fall within the range. It works perfect for my needs, but maybe it will not always work since it isn't the proper way to do it?

Reply With Quote
  #4  
Old January 19th, 2013, 10:15 AM
f_razzoli f_razzoli is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Location: Italy
Posts: 36 f_razzoli User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 48 sec
Reputation Power: 1
I'm glad you enjoyd Italy!

Ok, let's suppose you have:

1994-01-01
1994-01-02
1994-01-03

Since you are GROUPing by year and month, all these dates fall inside the same group. So, which one should be selected by MySQL? It chooses one randomly. This behavior may be ok for you, and I think it works fine as long as you use MySQL. However it is not logically correct and is not standard. That's why I suggested you to change your query

If you need to change indexes... just do it All your queries will still work. Just check the EXPLAIN output.

Reply With Quote
  #5  
Old January 19th, 2013, 01:07 PM
NotionCommotion NotionCommotion is offline
Contributing User
Click here for more information.
 
Join Date: Sep 2006
Posts: 1,464 NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level)NotionCommotion User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 6 h 21 m 9 sec
Reputation Power: 526
Thanks again f_razzoli,

I think I agree with you and will change my query. While it currently works perfectly, whenever I do something that is not standard, it ends up biting me.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > GROUP BY date question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap