title: Double aggregation of queryset in django
body: <p>I need perform what is essentially a double aggregation. First, I need to get the average for the same events from different sources per ticker. Next, I need to get the sum of different events per ticker.</p>
  <p>I can't figure out how to do this using Django. I've tried:</p>
  <pre><code>query1 = queryset.values('ticker', 'event_id').annotate(value_usd=Avg('value_usd'), shares=Avg('shares'), days_to_trade=Avg('days_to_trade'), num_trades=Count('ticker'))
  query2 = query1.values('ticker').annotate(value_usd=Sum('value_usd'), shares=Sum('shares'), days_to_trade=Sum('days_to_trade'), num_trades=Count('security'))
  </code></pre>
  <p>but I receive an error <code>django.core.exceptions.FieldError: Cannot compute Sum('value_usd'): 'value_usd' is an aggregate</code>.</p>
  <p>My raw data looks like this:</p>
  <pre><code>[{
      'ticker': 'ACVA-US', 
      'source': 'INTERNAL',
      'event_id': 300, 
      'value_usd': 3000.0, 
      'shares': 300.0, 
      'days_to_trade': 3.0, 
      'num_trades': 1
  },
  {
      'ticker': 'ACVA-US',
      'source': 'INTERNAL',  
      'event_id': 200, 
      'value_usd': 2000.0, 
      'shares': 200.0, 
      'days_to_trade': 2.0, 
      'num_trades': 1
  },
  {
      'ticker': 'ACVA-US',
      'source': 'EXTERNAL',  
      'event_id': 200, 
      'value_usd': 1000.0, 
      'shares': 100.0, 
      'days_to_trade': 1.0, 
      'num_trades': 1
  }]
  </code></pre>
  <p>I need to perform what is essentially a double-aggregation. First, I need to take the average of items with the same ticker+event, which would look like:</p>
  <pre><code>[{
      'ticker': 'ACVA-US', 
      'event_id': 300, 
      'value_usd': 3000.0, 
      'shares': 300.0, 
      'days_to_trade': 3.0, 
      'num_trades': 1
  },
  {
      'ticker': 'ACVA-US',
      'event_id': 200, 
      'value_usd': 1500.0, 
      'shares': 150.0, 
      'days_to_trade': 1.5, 
      'num_trades': 2
  }]
  </code></pre>
  <p>Next, I need to sum up items across tickers, which would look like:</p>
  <pre><code>[{
      'ticker': 'ACVA-US', 
      'value_usd': 4500.0, 
      'shares': 450.0, 
      'days_to_trade': 4.5, 
      'num_trades': 3
  }]
  </code></pre>
  <p>I was hoping to do this without having to create a db view that performs the first aggregation, but so far I can't figure it out. I need to perform this at the queryset level without dropping into raw SQL because a bunch of other filtering occurs before and after this. Any advice?</p>