Re: Re: getting max and min
On 10/20/06, DavidA <[EMAIL PROTECTED]> wrote: > I'm curious how you would solve this problem using SQLAlchemy. I have > _many_ needs for aggregation, subselects, and other things that are not > easy or possible to do in Django's ORM, but I've never seen an ORM that > does this (without just providing some sort of pass-thru SQL). Here's how SQLAlchemy handles subqueries: http://www.sqlalchemy.org/docs_03/sqlconstruction.myt#sql_subqueries -- "May the forces of evil become confused on the way to your house." -- George Carlin --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: getting max and min
Frankie Robertson wrote: > Yes, dropping down is a good thing. You'll never be able to do > everything with django's simple ORM. Raw SQL is a 'good thing', > honest. Once we get SQLAlchemy we can drop to that some of the time > but until then SQL is the way forward. Slightly OT, but... I'm curious how you would solve this problem using SQLAlchemy. I have _many_ needs for aggregation, subselects, and other things that are not easy or possible to do in Django's ORM, but I've never seen an ORM that does this (without just providing some sort of pass-thru SQL). I've been thinking about writing some wrappers/helpers/extensions to the Django DB API to help with this but maybe its easier to just use SQLAlchemy? Pointers or examples would be greatly appreciated. Thanks, -Dave --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: getting max and min
thank you all! I will do it with raw SQL . thanks for the code. I hope SQLAlchemy will be implemented soon in django. thank you! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: getting max and min
On 20/10/06, DavidA <[EMAIL PROTECTED]> wrote: > > > Russell Keith-Magee wrote: > > On 10/19/06, zenx <[EMAIL PROTECTED]> wrote: > > > > > > I want to get the maximum and the minimum values of various numbers. Is > > > the following method the best way to do it? > > > > The most efficient way would be to use SQL; this way, the min and max > > would fall out as the result of a single query. > > > > Unfortunately, this requires some fairly complex use of aggregate > > clauses. The only support that Django provides for these clauses is as > > part of the extra clause. > > I take it 'tag' is a ManyToMany field in 'artista'? Assuming your app > is named 'app', you should be able to do something like this: > > from django.db import connection > ... > cursor = connection.cursor() > cursor.execute(""" >select min(c), max(c) >from (select tag_id, count(*) as c from app_artista_tags group by > tag_id) s""") > (min_c, max_c) = cursor.fetchall()[0] > cusor.close() > > I find some things are easier (and faster) to do in SQL so its not > necessarilly a bad thing to drop down to custom SQL. Yes, dropping down is a good thing. You'll never be able to do everything with django's simple ORM. Raw SQL is a 'good thing', honest. Once we get SQLAlchemy we can drop to that some of the time but until then SQL is the way forward. > -Dave > > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: getting max and min
Russell Keith-Magee wrote: > On 10/19/06, zenx <[EMAIL PROTECTED]> wrote: > > > > I want to get the maximum and the minimum values of various numbers. Is > > the following method the best way to do it? > > The most efficient way would be to use SQL; this way, the min and max > would fall out as the result of a single query. > > Unfortunately, this requires some fairly complex use of aggregate > clauses. The only support that Django provides for these clauses is as > part of the extra clause. I take it 'tag' is a ManyToMany field in 'artista'? Assuming your app is named 'app', you should be able to do something like this: from django.db import connection ... cursor = connection.cursor() cursor.execute(""" select min(c), max(c) from (select tag_id, count(*) as c from app_artista_tags group by tag_id) s""") (min_c, max_c) = cursor.fetchall()[0] cusor.close() I find some things are easier (and faster) to do in SQL so its not necessarilly a bad thing to drop down to custom SQL. -Dave --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: getting max and min
On 10/19/06, zenx <[EMAIL PROTECTED]> wrote: > > I want to get the maximum and the minimum values of various numbers. Is > the following method the best way to do it? The most efficient way would be to use SQL; this way, the min and max would fall out as the result of a single query. Unfortunately, this requires some fairly complex use of aggregate clauses. The only support that Django provides for these clauses is as part of the extra clause. Yours, Russ Magee %-) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: getting max and min
zenx wrote: > hi, > > just tried this but doesn't work: > > q = ArtistaTag.objects.all() > nums=[''] > for tag in q: > num = tag.artista_set.count() > nums.append(num) > > max_art = max(nums) > min_art = min(nums) > > i get a TypeError unsupported operand type(s) for -: 'str' and 'long' :( Why not use the list comprehension in python: nums = [tag.artista_set.count() for tag in q] max_art = max(number) min_art = min(number) Ian. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: getting max and min
On Oct 19, 2006, at 3:21 PM, zenx wrote: > > hi, > > just tried this but doesn't work: > > q = ArtistaTag.objects.all() > nums=[''] > for tag in q: > num = tag.artista_set.count() > nums.append(num) > > max_art = max(nums) > min_art = min(nums) > > i get a TypeError unsupported operand type(s) for -: 'str' and > 'long' :( You initialize nums as an array with an empty string as the first member. You just need to initialize it like this: nums = [] Don --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: getting max and min
hi, just tried this but doesn't work: q = ArtistaTag.objects.all() nums=[''] for tag in q: num = tag.artista_set.count() nums.append(num) max_art = max(nums) min_art = min(nums) i get a TypeError unsupported operand type(s) for -: 'str' and 'long' :( --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Re: getting max and min
On Oct 19, 2006, at 5:03 AM, zenx wrote: > > I want to get the maximum and the minimum values of various > numbers. Is > the following method the best way to do it? Python already has min and max functions available. Just feed as many values in a tuple and it will return the min or max values; min_value = min((5,7,3)) produces min_value = 3 Don --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
getting max and min
I want to get the maximum and the minimum values of various numbers. Is the following method the best way to do it? for tag in q: num = tag.artista_set.count() if num_ant: if num < num_ant: min_art = num else: num_ant = num if num > max_art: max_art = num Thank you! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---