(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
[inherit('psqlitedir:sqlite', 'psqlitedir:psqlite')]
program cmplx(input, output);

var
   con : sqlite_ptr;
   sel : sqlite_stmt_ptr;

begin
   writeln('Pascal - top customers by sales:');
   con := psqlite_open('test.db');
   sel := psqlite_prepare(con, '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');
   while psqlite_step_query(sel) do begin
      writeln(psqlite_column_text(sel, 0), ' : ', psqlite_column_int(sel, 1):1);
   end;
   psqlite_finalize(sel);
   psqlite_close(con);
end.
