Skip to content

Instantly share code, notes, and snippets.

@hipertracker
Created August 23, 2018 10:20
Show Gist options
  • Select an option

  • Save hipertracker/cfbf9a34c683c0341a7f2c3b8ecf7574 to your computer and use it in GitHub Desktop.

Select an option

Save hipertracker/cfbf9a34c683c0341a7f2c3b8ecf7574 to your computer and use it in GitHub Desktop.
Example raw sql in Django
from django.db import connection
def categories():
sql = '''
SELECT
SUM(pageview) AS pageview__sum,
substring(url from '^/[^/]+/') AS category
FROM portal_tracker
GROUP BY substring(url from '^/[^/]+/')
HAVING substring(url from '^/[^/]+/') IS NOT NULL
ORDER BY pageview__sum DESC
'''
with connection.cursor() as c:
c.execute(sql)
return c.fetchall()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment