avg sales

 avatar
unknown
plain_text
14 days ago
848 B
3
Indexable
public function get_average_sales() {
    // First, we define a subquery. In CodeIgniter, this can be achieved using the following syntax:
    $this->db->select('tbl_sales_product.sale_id, COUNT(tbl_sales_product.id) AS sale_count');
    $this->db->from('tbl_sales_product');
    $this->db->join('tbl_sales', 'tbl_sales.id = tbl_sales_product.sale_id', 'inner');
    $this->db->where('tbl_sales_product.subtotal >', 0);
    $this->db->where('tbl_sales.status !=', 'delete');
    $this->db->group_by('tbl_sales_product.sale_id');
    $subQuery = $this->db->get_compiled_select();

    // Now, use the result of the subquery to calculate the average.
    $query = $this->db->query("
        SELECT 
            AVG(sale_count) AS average_sales
        FROM 
            ($subQuery) AS sales_counts
    ");

    return $query->row()->average_sales;
}
Leave a Comment