How to select by WEEK and count it in postgresql and Django
<p>I'm trying to select by week and counting how many tickets were sold on that week.</p>

<p>i select the tickets using EVENT ID.</p>

<pre><code>WHERE EVENT ID 148
SAMPLE DATA: TICKETS TABLE
-------------------------------------------------

    "General";0;"2016-09-02 17:50:45.644381+00"
    "General";0;"2016-09-03 21:05:54.830366+00"
    "General";0;"2016-09-02 18:21:33.976451+00"
    "Early Bird";500;"2016-09-09 19:15:33.721279+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "Post Secondary Student";1000;"2016-09-06 14:46:53.90927+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "General";0;"2016-09-01 23:50:05.034436+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "Post Secondary Student";1000;"2016-09-06 14:46:53.90927+00"
    "Post Secondary Student";1000;"2016-09-06 14:46:53.90927+00"
    "General";0;"2016-09-03 18:39:15.571188+00"
    "General";0;"2016-09-07 20:14:35.959517+00"
    "General";0;"2016-09-03 21:33:04.349198+00"
    "General";0;"2016-09-07 18:21:22.220223+00"
    "General";0;"2016-09-01 23:34:55.773516+00"
    "General";0;"2016-09-01 23:42:15.498778+00"
    "Early Bird";500;"2016-09-09 19:15:33.721279+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "RSVP";0;"2016-09-27 21:27:33.378934+00"
    "RSVP";0;"2016-09-14 22:23:04.922607+00"
    "RSVP";0;"2016-09-14 22:23:04.922607+00"
    "General Admission";0;"2016-09-23 15:35:54.972803+00"
    "General Admission";0;"2016-09-23 15:35:54.972803+00"
    "RSVP";0;"2016-09-14 22:23:04.922607+00"
    "RSVP";0;"2016-09-14 22:23:04.922607+00"
    "General";1000;"2016-09-09 19:15:33.72771+00"
    "General Admission";0;"2016-09-23 15:35:54.972803+00"
    "General Admission";0;"2016-09-23 15:35:54.972803+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "RSVP";0;"2016-09-14 22:23:04.922607+00"
    "RSVP";0;"2016-09-14 22:23:04.922607+00"
    "General Admission";0;"2016-09-23 15:35:54.972803+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "Youth";0;"2016-09-06 14:46:53.903704+00"
    "General Admission";0;"2016-09-23 15:35:54.972803+00"
    "General Admission";0;"2016-09-23 15:35:54.972803+00"
    "Free Admission";0;"2016-10-03 19:12:12.965369+00"
    "Free Admission";0;"2016-10-06 19:00:25.926406+00"
    "Free Admission";0;"2016-10-06 19:00:25.926406+00"
</code></pre>

<p>Any suggestions how i would achieve that?</p>

<p>I DID THIS TO FIND AND COUNT BY DAY:</p>

<pre><code>Ticket.objects.filter(event_id=event_id, event_ticket_id=ticket_type.id, refunded=False).extra(where=('created',),
                            select={'date_sold':'date(created)'}).values('date_sold').annotate(sold_count=Count('id'))
</code></pre>

<p>but could not do by week.</p>

<p>Thank you</p>

<p>Here is a solution that I used in a similar situation.
I used a Raw SQL query however.</p>

<pre><code>Ticket.objects.raw('''SELECT COUNT(app_ticket.id) as id, app_ticket.name, EXTRACT(WEEK FROM app_ticket.created) as week, EXTRACT(YEAR FROM app_ticket.created) as YEAR
      FROM app_ticket
      WHERE app_ticket.event_id = %s
      GROUP BY app_ticket.name, EXTRACT(WEEK FROM app_ticket.created), EXTRACT(YEAR FROM app_ticket.created), app_ticket.id
      ORDER BY EXTRACT(WEEK FROM app_ticket.created), EXTRACT(YEAR FROM app_ticket.created)''', [1])
</code></pre>

<p>Hope it helps.</p>

python django postgresql date