Note that there are some explanatory texts on larger screens.

plurals
  1. POMerge two annotated results into one dictionary
    text
    copied!<p>I have to annotated results:</p> <pre><code>cred_rec = Disponibilidad.objects.values('mac__mac', 'int_mes').annotate(tramites=Count('fuar'), recibidas=Count('fecha_disp_mac') cred14 = Disponibilidad.objects.filter(int_disponible__lte=14).values('mac__mac', 'int_mes').annotate(en14=Count('fuar')) </code></pre> <p>Both have the same keys <code>'mac__mac'</code> and <code>'int_mes'</code>, what I want is to create a new dictionary with the keys in <code>cred_red</code>, plus <code>en14</code> from <code>cred14</code>.</p> <p>I try some <a href="https://stackoverflow.com/questions/720966/django-how-to-merge-two-related-querysets-in-django-0-96">answers</a> <a href="https://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view">found</a> <a href="https://stackoverflow.com/questions/5945912/how-to-get-the-difference-of-two-querysets-in-django">here</a>, but I missing something.</p> <p>Thanks.</p> <p><strong>EDIT.</strong> After some tries and errors, I got this:</p> <pre><code>for linea in cred_rec: clave = (linea['mac__mac'], linea['int_mes']) for linea2 in cred14: clave2 = (linea2['mac__mac'], linea2['int_mes']) if clave2 == clave: linea['en14'] = linea2['en14'] linea['disp'] = float(linea2['en14'])/linea['recibidas']*100 </code></pre> <p>Now, I have to ask for a better solution. Thanks again.</p> <p>======= <strong>EDIT</strong> This is how the input looks like:</p> <pre><code>fuar, mac_id, int_mes, int_disponible, int_exitoso, fecha_tramite, fecha_actualiza_pe, fecha_disp_mac 1229012106349,1,7,21,14,2012-07-02 08:33:54.0,2012-07-16 17:33:21.0,2012-07-23 08:01:22.0 1229012106350,1,7,25,17,2012-07-02 09:01:25.0,2012-07-19 17:45:57.0,2012-07-27 17:45:59.0 1229012106351,1,7,21,14,2012-07-02 09:15:12.0,2012-07-16 19:14:35.0,2012-07-23 08:01:22.0 1229012106352,1,7,24,16,2012-07-02 09:25:19.0,2012-07-18 07:52:18.0,2012-07-26 16:04:11.0 ... a few thousand lines dropped ... </code></pre> <p>The <code>fuar</code> is like an order_id; <code>mac__mac</code> is like the <code>site_id</code>, mes is <code>month</code>; <code>int_disponible</code> is the timedelta between <code>fecha_tramite</code> and <code>fecha_disp_mac</code>; <code>int_exitoso</code> is the timedelta between <code>fecha_tramite</code> and <code>fecha_actualiza_pe</code>.</p> <p>The output is like this:</p> <pre><code>mac, mes, tramites, cred_rec, cred14, % rec, % en 14 1, 7, 2023, 2006, 1313, 99.1596638655, 65.4536390828 1, 8, 1748, 1182, 1150, 67.6201372998, 97.2927241963 2, 8, 731, 471, 441, 64.4322845417, 93.6305732484 3, 8, 1352, 840, 784, 62.1301775148, 93.3333333333 </code></pre> <ul> <li><em>tramites</em> is the sum of all orders (<code>fuar</code>) in a month</li> <li><em>cred_rec</em> cred is our product, in theory for each <em>fuar</em> there is a <em>cred</em>, <code>cred_rec</code> is the sum of all cred produced in a month</li> <li><em>cred_14</em> is the sum of all cred made in 14 days</li> <li><em>% rec</em> the relationship between fuar received and cred produced, in %</li> <li><em>% en 14</em> is the relationship between the cred produced and the cred produced in time</li> </ul> <p>I will use this table in a <a href="http://code.google.com/apis/ajax/playground/?type=visualization#annotated_time_line" rel="nofollow noreferrer">Annotated Time Line</a> chart or a <a href="http://code.google.com/apis/ajax/playground/?type=visualization#combo_chart" rel="nofollow noreferrer">Combo Chart</a> from Google Charts to show the performance of our manufacturation process.</p> <p>Thanks for your time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload