Thanks Tony. I'm ideally looking for a way to do this through models
rather than SQL

On Nov 16, 11:28 pm, Tony Czeh <tony.c...@gmail.com> wrote:
> On 11/16/09 1:12 PM, despy wrote:
>
>
>
>
>
> > Hi,
>
> > I'm trying to get my head around a complex aggregate query and I could
> > do with some help. Say I have the following models
>
> > StockMarket
> > |
> > Stock
> > |
> > StockPrice
>
> > If StockPrice has price and date fields, and one price entry for every
> > day for every stock how would I write a query to get the average price
> > for a given stockmarket for the last six months?
>
> > Thanks
>
> >Greig
>
> > --
>
> > You received this message because you are subscribed to the Google Groups 
> > "Django users" group.
> > To post to this group, send email to django-us...@googlegroups.com.
> > To unsubscribe from this group, send email to 
> > django-users+unsubscr...@googlegroups.com.
> > For more options, visit this group 
> > athttp://groups.google.com/group/django-users?hl=.
>
> Something along these lines should work in SQL:
>
> SELECT s.stock_symbol
>       , AVG(sp.price) AS average_price
> FROM Stock s
>       LEFT JOIN StockPrice sp
>              ON s.id = sp.stock_id
> WHERE sp.date BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 6 MONTH)
>                AND CURRENT_DATE
> GROUP BY s.stock_symbol

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.


Reply via email to