If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > need help with query please

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-19-10, 09:35
topcatxx topcatxx is offline
Registered User
 
Join Date: Nov 2009
Posts: 5
need help with query please

Hi,

I'm trying to write a query that returns 2 columns, the date and the number of records that were added to the table on that date. I'm really stuck though because the date is in the form of a timestamp which also includes the time.

I thought the query would be something like:

Code:
SELECT DATE_FORMAT( rstf_created, '%e %d %Y' ) , COUNT( *  )
FROM `rst_applicants`
GROUP BY DATE( `rstf_created` )
This does not work at all however, I get about 6000 records under count for a date of NULL, and about 10 records for dates in the future with 1 count each.

Can anyone give me a pointer, I'm guessing that my overall logic is off based on the results I'm getting but I also have no idea how to get MySQL to just use the date portion of the timestamp either.

Any help would be greatly appreciated.


Many thanks.
Reply With Quote
  #2 (permalink)  
Old 04-19-10, 10:49
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
When you GROUP BY you must use the same syntax as in the SELECT i.e. DATE_FORMAT( rstf_created, '%e %d %Y' )
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 04-19-10, 11:06
topcatxx topcatxx is offline
Registered User
 
Join Date: Nov 2009
Posts: 5
Hi Ronan,

Thanks for taking the time to reply.

I tried your suggestion (assuming I understood you correctly) to no avail.

The query now looks like this:

Code:
SELECT DATE_FORMAT( rstf_created, '%e %d %Y' ) , COUNT( *  )
FROM `rst_applicants`
GROUP BY DATE_FORMAT(rstf_created, '%e %d %Y' )
But I get the same results back, one field of date 'NULL' with a count of 6000 records and about 10 records with dates set in the future with a count of 1 each. Also I have just noticed that one of the dates is 31 31 2012 which obviously is impossible.

Am I using the functions correctly - I assume that you can pass a timestamp to date_format without converting it into a datetime object first?

Thanks again.

Tony
Reply With Quote
  #4 (permalink)  
Old 04-19-10, 11:14
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Hi Tony,

I think the problem is with %e and %d which give the same i.e. the day of the month. I think you need to look at %e %m and %Y.

Ronan
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 04-20-10, 03:01
topcatxx topcatxx is offline
Registered User
 
Join Date: Nov 2009
Posts: 5
Thanks again Ronan - misread the docs there!

I spotted the rather handy FROM_UNIXTIME() function as well, in the end the following query did the trick:

Code:
SELECT DATE_FORMAT(FROM_UNIXTIME(rstf_created),'%e %m %Y'), COUNT(*) FROM `rst_applicants` GROUP BY DATE_FORMAT(FROM_UNIXTIME(rstf_created),'%e %m %Y')
Cheers for your time.


Tony
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On