Little help for a query with average

This is a discussion on Little help for a query with average within the MySQL Database forums, part of the Database Forums category; Hello, I need a little help... I have a very simple table: date amount 2008-01-01 1000 2008-01-...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008
b.buz
 
Posts: n/a
Default Little help for a query with average

Hello, I need a little help...

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 help me?

Thanks to everybody!!


Reply With Quote
  #2 (permalink)  
Old 04-09-2008
ZeldorBlat
 
Posts: n/a
Default Re: Little help for a query with average

On Apr 9, 8:41 am, "b.buz" <b....@email.it> wrote:
> Hello, I need a little help...
>
> 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 help 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
Reply With Quote
  #3 (permalink)  
Old 04-09-2008
b.buz
 
Posts: n/a
Default Re: Little help 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 help 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!!
Reply With Quote
  #4 (permalink)  
Old 04-09-2008
Michael Austin
 
Posts: n/a
Default Re: Little help for a query with average

ZeldorBlat wrote:
> On Apr 9, 8:41 am, "b.buz" <b....@email.it> wrote:
>> Hello, I need a little help...
>>
>> 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 help 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 help, but, should be at least some
attempt to figure it out...
Reply With Quote
  #5 (permalink)  
Old 04-10-2008
ZeldorBlat
 
Posts: n/a
Default Re: Little help 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 help...

>
> >> 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 help 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 help, 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.
Reply With Quote
  #6 (permalink)  
Old 04-11-2008
Michael Austin
 
Posts: n/a
Default Re: Little help 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 help...
>>>> 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 help 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 help, 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).


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 01:00 AM.


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