Unix Technical Forum

Summing up a total for a Year

This is a discussion on Summing up a total for a Year within the MySQL forums, part of the Database Server Software category; --> Hi, Seem to be getting stuck, I think there is an easy way to do what I want, I ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 04:09 PM
Barlymasher
 
Posts: n/a
Default Summing up a total for a Year

Hi,

Seem to be getting stuck, I think there is an easy way to do what I
want, I have just not found it yet.
I have data for 10 years, I am looking to get a SUM of a field for a
particular year.

For example:

SELECT SUM(ounces) as ounces
FROM beverages

give me back one result, the total ounces for all 10 years.

If I do this:

SELECT date, SUM(ounces) as ounces
FROM beverages
WHERE YEAR(date) = 1998
GROUP BY date
ORDER BY date DESC

I get back multiple rows for the year 1998, when I really only want
the one value that is the total SUM for all ounces during the year
1998.

beverages has 4 fields, location, date, ounces and type.

thanks for any help!

O
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 04:09 PM
Barlymasher
 
Posts: n/a
Default Re: Summing up a total for a Year

On Apr 16, 3:01 pm, Barlymasher <theo...@gmail.com> wrote:
> Hi,
>
> Seem to be getting stuck, I think there is an easy way to do what I
> want, I have just not found it yet.
> I have data for 10 years, I am looking to get a SUM of a field for a
> particular year.
>
> For example:
>
> SELECT SUM(ounces) as ounces
> FROM beverages
>
> give me back one result, the total ounces for all 10 years.
>
> If I do this:
>
> SELECT date, SUM(ounces) as ounces
> FROM beverages
> WHERE YEAR(date) = 1998
> GROUP BY date
> ORDER BY date DESC
>
> I get back multiple rows for the year 1998, when I really only want
> the one value that is the total SUM for all ounces during the year
> 1998.
>
> beverages has 4 fields, location, date, ounces and type.
>
> thanks for any help!
>
> O


Also, I have tried


SELECT SUM(ounces) as ounces
FROM beverages
WHERE YEAR(date) = 1998
GROUP BY ounces



O
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 04:09 PM
Paul Lautman
 
Posts: n/a
Default Re: Summing up a total for a Year

Barlymasher wrote:
> On Apr 16, 3:01 pm, Barlymasher <theo...@gmail.com> wrote:
>> Hi,
>>
>> Seem to be getting stuck, I think there is an easy way to do what I
>> want, I have just not found it yet.
>> I have data for 10 years, I am looking to get a SUM of a field for a
>> particular year.
>>
>> For example:
>>
>> SELECT SUM(ounces) as ounces
>> FROM beverages
>>
>> give me back one result, the total ounces for all 10 years.
>>
>> If I do this:
>>
>> SELECT date, SUM(ounces) as ounces
>> FROM beverages
>> WHERE YEAR(date) = 1998
>> GROUP BY date
>> ORDER BY date DESC
>>
>> I get back multiple rows for the year 1998, when I really only want
>> the one value that is the total SUM for all ounces during the year
>> 1998.
>>
>> beverages has 4 fields, location, date, ounces and type.
>>
>> thanks for any help!
>>
>> O

>
> Also, I have tried
>
>
> SELECT SUM(ounces) as ounces
> FROM beverages
> WHERE YEAR(date) = 1998
> GROUP BY ounces
>
>
>
> O


Try:
SELECT SUM(ounces) as ounces
FROM beverages
WHERE YEAR(date) = 1998
GROUP BY YEAR(date)


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 04:09 PM
ThanksButNo
 
Posts: n/a
Default Re: Summing up a total for a Year

On Apr 16, 12:09 pm, Barlymasher <theo...@gmail.com> wrote:
> On Apr 16, 3:01 pm, Barlymasher <theo...@gmail.com> wrote:
>
>
>
> > Hi,

>
> > Seem to be getting stuck, I think there is an easy way to do what I
> > want, I have just not found it yet.
> > I have data for 10 years, I am looking to get a SUM of a field for a
> > particular year.

>
> > For example:

>
> > SELECT SUM(ounces) as ounces
> > FROM beverages

>
> > give me back one result, the total ounces for all 10 years.

>
> > If I do this:

>
> > SELECT date, SUM(ounces) as ounces
> > FROM beverages
> > WHERE YEAR(date) = 1998
> > GROUP BY date
> > ORDER BY date DESC

>
> > I get back multiple rows for the year 1998, when I really only want
> > the one value that is the total SUM for all ounces during the year
> > 1998.

>
> > beverages has 4 fields, location, date, ounces and type.

>
> > thanks for any help!

>
> > O

>
> Also, I have tried
>
> SELECT SUM(ounces) as ounces
> FROM beverages
> WHERE YEAR(date) = 1998
> GROUP BY ounces
>
> O


Just to clarify the "group by" clause -- you can not "sum" and "group
by" on the same column. It doesn't make any sense.

What you can do is "group by" something else. Such as, e.g:

SELECT sum(ounces) FROM beverages
WHERE [other filters]
GROUP BY type

Which might result something like:

type sum(ounces)
Coke 8383
Sprite 1004
OJ 92883

etc. Meaning that, of all the beverages, Coke summed up to 8,383
ounces. Etc.

Actually, if you really only want a single "sum" result for a single
year, you ought to be able to do this:

SELECT SUM(ounces) FROM beverages
WHERE YEAR(date) = 1998

As you describe the problem, "group by" shouldn't be necessary at all.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:30 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com