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 > Little for a query with average
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Little for a query with average

Réponse
 
LinkBack Outils de la discussion
Vieux 09/04/2008, 13h41   #1
b.buz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Little for a query with average

Hello, I need a little ...

I have a very simple table:

date amount
2008-01-01 1000
2008-01-01 124
2008-01-02 333
2008-01-02 120
.......
2009-07-06 1023
2009-07-07 144
......

I need a simple query that gives me the average of amount for today,
the current month, the previous month and the current year.

A result like

today_avg month_avg lastmonth_avg
1023 989 789

can anyone me?

Thanks to everybody!!


  Réponse avec citation
Vieux 09/04/2008, 15h29   #2
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Little for a query with average

On Apr 9, 8:41 am, "b.buz" <b....@email.it> wrote:
> Hello, I need a little ...
>
> I have a very simple table:
>
> date amount
> 2008-01-01 1000
> 2008-01-01 124
> 2008-01-02 333
> 2008-01-02 120
> ......
> 2009-07-06 1023
> 2009-07-07 144
> .....
>
> I need a simple query that gives me the average of amount for today,
> the current month, the previous month and the current year.
>
> A result like
>
> today_avg month_avg lastmonth_avg
> 1023 989 789
>
> can anyone me?
>
> Thanks to everybody!!


Try this (untested):

select avg(case when date = curdate() then amount else null end)
today_avg,
avg(case when month(date) = month(curdate()) and year(date) =
year(curdate()) then amount else null end) month_avg,
avg(case when month(date) = month(date_sub(curdate(), interval
1 month)) and year(date) = year(date_sub(curdate(), interval 1 month))
then amount else null end) lastmonth_avg,
avg(case when year(date) = year(curdate()) then amount else
null end) year_avg
from yourTable
  Réponse avec citation
Vieux 09/04/2008, 17h01   #3
b.buz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Little for a query with average


> > I need a simple query that gives me the average of amount for today,
> > the current month, the previous month and the current year.

>
> > A result like

>
> > today_avg month_avg lastmonth_avg
> > 1023 989 789

>
> > can anyone me?

>
> > Thanks to everybody!!

>
> Try this (untested):
>
> select avg(case when date = curdate() then amount else null end)
> today_avg,
> avg(case when month(date) = month(curdate()) and year(date) =
> year(curdate()) then amount else null end) month_avg,
> avg(case when month(date) = month(date_sub(curdate(), interval
> 1 month)) and year(date) = year(date_sub(curdate(), interval 1 month))
> then amount else null end) lastmonth_avg,
> avg(case when year(date) = year(curdate()) then amount else
> null end) year_avg
> from yourTable


Thanks from Italy!!
  Réponse avec citation
Vieux 09/04/2008, 22h29   #4
Michael Austin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Little for a query with average

ZeldorBlat wrote:
> On Apr 9, 8:41 am, "b.buz" <b....@email.it> wrote:
>> Hello, I need a little ...
>>
>> I have a very simple table:
>>
>> date amount
>> 2008-01-01 1000
>> 2008-01-01 124
>> 2008-01-02 333
>> 2008-01-02 120
>> ......
>> 2009-07-06 1023
>> 2009-07-07 144
>> .....
>>
>> I need a simple query that gives me the average of amount for today,
>> the current month, the previous month and the current year.
>>
>> A result like
>>
>> today_avg month_avg lastmonth_avg
>> 1023 989 789
>>
>> can anyone me?
>>
>> Thanks to everybody!!

>
> Try this (untested):
>
> select avg(case when date = curdate() then amount else null end)
> today_avg,
> avg(case when month(date) = month(curdate()) and year(date) =
> year(curdate()) then amount else null end) month_avg,
> avg(case when month(date) = month(date_sub(curdate(), interval
> 1 month)) and year(date) = year(date_sub(curdate(), interval 1 month))
> then amount else null end) lastmonth_avg,
> avg(case when year(date) = year(curdate()) then amount else
> null end) year_avg
> from yourTable



Just hope you didn't supply him with the answer to homework/schoolwork.
I like to at least see some attempt on their part to try and resolve
the issue. Not that they don't need , but, should be at least some
attempt to figure it out...
  Réponse avec citation
Vieux 10/04/2008, 17h24   #5
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Little for a query with average

On Apr 9, 5:29 pm, Michael Austin <maus...@firstdbasource.com> wrote:
> ZeldorBlat wrote:
> > On Apr 9, 8:41 am, "b.buz" <b....@email.it> wrote:
> >> Hello, I need a little ...

>
> >> I have a very simple table:

>
> >> date amount
> >> 2008-01-01 1000
> >> 2008-01-01 124
> >> 2008-01-02 333
> >> 2008-01-02 120
> >> ......
> >> 2009-07-06 1023
> >> 2009-07-07 144
> >> .....

>
> >> I need a simple query that gives me the average of amount for today,
> >> the current month, the previous month and the current year.

>
> >> A result like

>
> >> today_avg month_avg lastmonth_avg
> >> 1023 989 789

>
> >> can anyone me?

>
> >> Thanks to everybody!!

>
> > Try this (untested):

>
> > select avg(case when date = curdate() then amount else null end)
> > today_avg,
> > avg(case when month(date) = month(curdate()) and year(date) =
> > year(curdate()) then amount else null end) month_avg,
> > avg(case when month(date) = month(date_sub(curdate(), interval
> > 1 month)) and year(date) = year(date_sub(curdate(), interval 1 month))
> > then amount else null end) lastmonth_avg,
> > avg(case when year(date) = year(curdate()) then amount else
> > null end) year_avg
> > from yourTable

>
> Just hope you didn't supply him with the answer to homework/schoolwork.
> I like to at least see some attempt on their part to try and resolve
> the issue. Not that they don't need , but, should be at least some
> attempt to figure it out...


Point taken. Some people are better at disguising them than others

I do think the "case statement inside an aggregate" is a very powerful
tool that many people overlook, though.
  Réponse avec citation
Vieux 11/04/2008, 00h00   #6
Michael Austin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Little for a query with average

ZeldorBlat wrote:
> On Apr 9, 5:29 pm, Michael Austin <maus...@firstdbasource.com> wrote:
>> ZeldorBlat wrote:
>>> On Apr 9, 8:41 am, "b.buz" <b....@email.it> wrote:
>>>> Hello, I need a little ...
>>>> I have a very simple table:
>>>> date amount
>>>> 2008-01-01 1000
>>>> 2008-01-01 124
>>>> 2008-01-02 333
>>>> 2008-01-02 120
>>>> ......
>>>> 2009-07-06 1023
>>>> 2009-07-07 144
>>>> .....
>>>> I need a simple query that gives me the average of amount for today,
>>>> the current month, the previous month and the current year.
>>>> A result like
>>>> today_avg month_avg lastmonth_avg
>>>> 1023 989 789
>>>> can anyone me?
>>>> Thanks to everybody!!
>>> Try this (untested):
>>> select avg(case when date = curdate() then amount else null end)
>>> today_avg,
>>> avg(case when month(date) = month(curdate()) and year(date) =
>>> year(curdate()) then amount else null end) month_avg,
>>> avg(case when month(date) = month(date_sub(curdate(), interval
>>> 1 month)) and year(date) = year(date_sub(curdate(), interval 1 month))
>>> then amount else null end) lastmonth_avg,
>>> avg(case when year(date) = year(curdate()) then amount else
>>> null end) year_avg
>>> from yourTable

>> Just hope you didn't supply him with the answer to homework/schoolwork.
>> I like to at least see some attempt on their part to try and resolve
>> the issue. Not that they don't need , but, should be at least some
>> attempt to figure it out...

>
> Point taken. Some people are better at disguising them than others
>
> I do think the "case statement inside an aggregate" is a very powerful
> tool that many people overlook, though.



the CASE statement in general is extremely powerful. Those more
familiar with Oracle seem to like the decode, but I find it is confusing
unless you are the one that wrote it. Some database-engine-specific
syntax (ie NOT ANSI standard) is not very support friendly. The person
who wrote it may well understand it, but those who have to support it in
production spend an inordinate amount of time trying to figure out what
they did. Same thing goes for (+) (Oracle outer join syntax).


  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 10h46.


É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,18325 seconds with 14 queries