sales-report.vala 1.1 KB

123456789101112131415161718192021222324252627
  1. using Invercargill.Expressions;
  2. using InvercargillSql.Orm;
  3. using InvercargillSql.Orm.Projections;
  4. /**
  5. * Aggregate projection demonstrating GROUP BY and aggregate functions.
  6. */
  7. public class SalesReport : Object {
  8. public string category { get; set; }
  9. public int64 total_orders { get; set; }
  10. public double total_revenue { get; set; }
  11. public double avg_order_value { get; set; }
  12. public SalesReport() {
  13. category = "";
  14. }
  15. public static void configure_projection(ProjectionBuilder<SalesReport> p) throws ProjectionError {
  16. p.source<Order>("o")
  17. .join<Product>("p", expr("o.product_id == p.id"))
  18. .group_by(expr("p.category"))
  19. .select<string>("category", expr("p.category"), (x, v) => x.category = v)
  20. .select<int64?>("total_orders", expr("COUNT(o.id)"), (x, v) => x.total_orders = v)
  21. .select<double?>("total_revenue", expr("SUM(o.total)"), (x, v) => x.total_revenue = v)
  22. .select<double?>("avg_order_value", expr("AVG(o.total)"), (x, v) => x.avg_order_value = v);
  23. }
  24. }