# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import sqlite3

print('Python - top customers by sales:')
con = sqlite3.connect('test.db')
c = con.cursor()
c.execute('''SELECT customers.name,SUM(orderlines.price) AS totalsales
             FROM customers
             LEFT JOIN orders ON customers.customer_id = orders.customer_id
             LEFT JOIN orderlines ON orders.order_id = orderlines.order_id
             WHERE orders.status = 'Delivered'
             GROUP BY customers.name
             HAVING totalsales > 10
             ORDER BY totalsales DESC
             LIMIT 3''')
for row in c.fetchall():
    print('%s : %d' % (row[0], row[1]))
con.commit()
con.close()
