PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Summing up a total for a Year
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Summing up a total for a Year

Réponse
 
LinkBack Outils de la discussion
Vieux 16/04/2008, 21h01   #1
Barlymasher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 !

O
  Réponse avec citation
Vieux 16/04/2008, 21h09   #2
Barlymasher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 !
>
> O


Also, I have tried


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



O
  Réponse avec citation
Vieux 16/04/2008, 21h56   #3
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 !
>>
>> 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)


  Réponse avec citation
Vieux 17/04/2008, 04h24   #4
ThanksButNo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 !

>
> > 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h40.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11190 seconds with 12 queries