logo       

Is it possible to combine these two queries?: msg#02217

DjangoUsers

Subject: Is it possible to combine these two queries?


Hi,

I have two queries as follows.


al_1 = Alert.objects.filter(creation_date__regex=today).values
('dimension1').annotate(Sum('metric1')).order_by('dimension1')

and

al_2 = Alert.objects.filter(creation_date__regex=yesterday).values
('dimension1').annotate(Sum('metric1')).order_by('dimension1')

They are almost the same except that al_1 is for today's data and al_2
is for yesterday's data. They summarize data in column "metric1" that
is grouped and ordered by "dimension1".

Sample data from al_1:

[{'metric1__sum': 0.0, 'dimension1': u'1'}, {'metric1__sum': 14.0,
'dimension1': u'110044'}, {'metric1__sum': 2658.0, 'dimension1':
u'110085'}, {'metric1__sum': 0.0, 'dimension1': u'110181'},
{'metric1__sum': 71.0, 'dimension1': u'110208'}, {'metric1__sum':
5850.0, 'dimension1': u'110219'}, {'metric1__sum': 97.0, 'dimension1':
u'110235'}, {'metric1__sum': 48253.0, 'dimension1': u'110251'}, ...]

and sample data from al_2:

[{'metric1__sum': 0.0, 'dimension1': u'1'}, {'metric1__sum': 16.0,
'dimension1': u'110044'}, {'metric1__sum': 2758.0, 'dimension1':
u'110085'}, {'metric1__sum': 67.0, 'dimension1': u'110208'},
{'metric1__sum': 5783.0, 'dimension1': u'110219'}, {'metric1__sum':
100.0, 'dimension1': u'110235'}, {'metric1__sum': 50688.0,
'dimension1': u'110251'},....]


What I need to do is to find a ratio value for each "dimension1"
between its today's value and yesterday's value. For example, for
dimension1 = '110085', I need to find 2758.0 / 2658.0 = 1.04.
Then I compare this value "1.04" with a threshold and process
further.

I know that I can use a for loop to traverse those results (al_1 and
al_2), get ratio values, compare them, etc. However, I wonder if there
is a way to do all this work with one query instead of using two
queries and one for loop?

Any ideas and suggestions?

Thanks so much.











--~--~---------~--~----~------------~-------~--~----~
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@xxxxxxxxxxxxxxxx
To unsubscribe from this group, send email to
django-users+unsubscribe@xxxxxxxxxxxxxxxx
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | Mail Home | sitemap | FAQ | advertise