getting months from days

This is a discussion on getting months from days within the MySQL Database forums, part of the Database Forums category; Hi, I'm using MySQL 5.0. If I have a table, DAILY_SALES, with columns DAY DATE, SALES FLOAT UNSIGNED, ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-29-2007
laredotornado@zipmail.com
 
Posts: n/a
Default getting months from days

Hi,

I'm using MySQL 5.0. If I have a table, DAILY_SALES, with columns

DAY DATE,
SALES FLOAT UNSIGNED,
USER_ID INTEGER,
PRIMARY KEY (DAY)

How would I write a query that given a user id and date range, returns
monthly sales totals? So, if the date range were '2006-01-01' to
'2006-12-31', my query result set would have twelve rows of data (and
you can assume there is at least one sale per month).

Thanks for your help, - Dave

Reply With Quote
  #2 (permalink)  
Old 01-29-2007
Gordon Burditt
 
Posts: n/a
Default Re: getting months from days

>I'm using MySQL 5.0. If I have a table, DAILY_SALES, with columns
>
>DAY DATE,
>SALES FLOAT UNSIGNED,
>USER_ID INTEGER,
>PRIMARY KEY (DAY)
>
>How would I write a query that given a user id and date range, returns
>monthly sales totals? So, if the date range were '2006-01-01' to
>'2006-12-31', my query result set would have twelve rows of data (and
>you can assume there is at least one sale per month).


This is somewhat clunky, but left(day,7) gives you the year and the month
without the day part. You can GROUP BY this. Also, I think DAY is a
keyword.

SELECT concat(left(`DAY`,7), '-01'), sum(SALES), USER_ID FROM DAILY_SALES
WHERE USER_ID = whatever GROUP BY left(`DAY`,7)

Reply With Quote
  #3 (permalink)  
Old 01-29-2007
Axel Schwenke
 
Posts: n/a
Default Re: getting months from days

gordonb.e1mm4@burditt.org (Gordon Burditt) wrote:
>>I'm using MySQL 5.0. If I have a table, DAILY_SALES, with columns
>>
>>DAY DATE,
>>SALES FLOAT UNSIGNED,
>>USER_ID INTEGER,
>>PRIMARY KEY (DAY)
>>
>>How would I write a query that given a user id and date range, returns
>>monthly sales totals? So, if the date range were '2006-01-01' to
>>'2006-12-31', my query result set would have twelve rows of data (and
>>you can assume there is at least one sale per month).

>
> This is somewhat clunky,


More than that.

> but left(day,7) gives you the year and the month
> without the day part.


The problem is, that you rely on the string representation of a DATE
column. This representation may change in the future. Many users asked
for a configurable default_date_format to be used for the automatic
conversion, so sooner or later this will come. So your query might work
or not, depending on the MySQL version and configuration.

After all, a DATE is a DATE and no string. There are functions around
to efficiently deal with DATE values. I.e. DATE_FORMAT('%Y-%m', `DAY`)
would have been a correct way to extract year and month from the `DAY`
column. IMNSHO it's even better to handle year and month as separate,
numeric entities:

SELECT YEAR(`DAY`) AS year,
MONTH(`DAY`) AS month,
SUM(SALES) AS total
FROM DAILY_SALES
WHERE USER_ID = ...
AND `DAY` BETWEEN ... AND ...
GROUP BY year, month


XL
--
Axel Schwenke, Support Engineer, MySQL AB

Online User Manual: http://dev.mysql.com/doc/refman/5.0/en/
MySQL User Forums: http://forums.mysql.com/
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:40 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0