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 > MySQL: Grouping date by quarterly and half yearly

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-19-11, 12:57
cancer10 cancer10 is offline
Registered User
 
Join Date: Mar 2008
Posts: 33
Question MySQL: Grouping date by quarterly and half yearly

Hello

I am looking for some mysql query assistance from you experts


I have a table that stores all the download logs of software on my website.

The table structure is as below:

Code:
CREATE TABLE `software_downloads` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `software_id` INT(10) DEFAULT NULL,
  `download_date` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MYISAM DEFAULT CHARSET=latin1
So what I am looking for is, when I generate reports, my customers should be able to group the dates by quarterly and half yearly. That means, if they choose the quarterly option, the report should list all the count of software download logs and group them by three months and six months in case if they choose the half-yearly option.


Thanks in advance for your help.
__________________
Interview Questions & Answers - www.focusinterview.com
http://outlineme.com/cancer10
Reply With Quote
  #2 (permalink)  
Old 09-19-11, 14:58
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
It depends on how you determine the quarter, do you mean Jan-Mar, Apr-Jun, July-Sep, Oct-Dec or from this point in time 3 months back, 6 months back etc?

If you look at FLOOR((DATE_FORMAT(download_date,'%m')-1)/3) will give you the quarter in which the download took place.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 09-19-11, 15:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
DATE_FORMAT(download_date,'%m') produces a string

use MONTH(download_date) instead, which produces a number, much more amenable to your numeric calculation

prettier, too

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 09-19-11, 22:22
cancer10 cancer10 is offline
Registered User
 
Join Date: Mar 2008
Posts: 33
Quote:
Originally Posted by it-iss.com View Post
It depends on how you determine the quarter, do you mean Jan-Mar, Apr-Jun, July-Sep, Oct-Dec or from this point in time 3 months back, 6 months back etc?

If you look at FLOOR((DATE_FORMAT(download_date,'%m')-1)/3) will give you the quarter in which the download took place.
Yes sir, I mean Jan-Mar, Apr-Jun, July-Sep, Oct-Dec, so would FLOOR((DATE_FORMAT(download_date,'%m')-1)/3) do this for me?

Thanks
__________________
Interview Questions & Answers - www.focusinterview.com
http://outlineme.com/cancer10
Reply With Quote
  #5 (permalink)  
Old 09-20-11, 02:02
cancer10 cancer10 is offline
Registered User
 
Join Date: Mar 2008
Posts: 33
One more query.

Please consider the following DDL

Code:
CREATE TABLE `software_downloads` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `software_id` INT(10) DEFAULT NULL,
  `download_by` VARCHAR(10) NOT NULL,
  `download_date` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MYISAM DEFAULT CHARSET=latin1

Here I added a new field "download_by", The values for this column will either be "admin" or "customer". so will it be possible to find out how many of the downloads are made by admin and how many of them are made by customers withing the same query?


Thanks
__________________
Interview Questions & Answers - www.focusinterview.com
http://outlineme.com/cancer10
Reply With Quote
  #6 (permalink)  
Old 09-20-11, 03:13
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
theres two elements to this
one element is the marshalling of the data, the query. it-iss.com's solution should work ok for that
..you may need two versions of this query
eg

FLOOR((MONTH(download_date,-1)/3) for quarters
FLOOR((MONTH(download_date,-1)/6) for half year

the other part is the actual report, but you don't mention what you are using as the front end, the user interface. you should be able to do the selection between monthly / quarterly / whatever time period you require.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #7 (permalink)  
Old 09-20-11, 03:22
cancer10 cancer10 is offline
Registered User
 
Join Date: Mar 2008
Posts: 33
Hi healdem

Thanks for your reply. Do you have a solution to my last post here ?
__________________
Interview Questions & Answers - www.focusinterview.com
http://outlineme.com/cancer10
Reply With Quote
  #8 (permalink)  
Old 09-20-11, 04:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by cancer10 View Post
Do you have a solution to my last post here ?
yeah, use GROUP BY download_by
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 09-27-11, 11:31
cancer10 cancer10 is offline
Registered User
 
Join Date: Mar 2008
Posts: 33
Thank you very much for your kind help.
__________________
Interview Questions & Answers - www.focusinterview.com
http://outlineme.com/cancer10
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