SQL to codeIgnitor

am new to code igniter but am good in SQL, i need help on how i can write the following query in codeIgniter

select sma_sales.date as date, 
       sma_users.username,
       sum(sma_sale_items.net_unit_price) as Revenue, 
       sum(sma_products.cost) as Cost,
       (sum(sma_sale_items.net_unit_price)-sum(sma_products.cost))as profit
From sma_sales, 
     sma_users, 
     sma_sale_items, 
     sma_products
WHERE sma_sales.id = sma_sale_items.sale_id
  AND sma_sale_items.product_id = sma_products.id
  AND sma_sales.created_by = sma_users.id
group by sma_sales.created_by, 
         cast(sma_sales.date as date);

Answer

I think raw query is convenient if it’s at that level.

Codeignither 4.x

$query = $db->query("YOUR QUERY");
foreach ($query->getResult() as $row)
{
    echo $row->title;
    echo $row->name;
    echo $row->body;
}

Codeignither 3.x

$queryStr =
" 
    select sma_sales.date as date, 
           sma_users.username,
           sum(sma_sale_items.net_unit_price) as Revenue, 
           sum(sma_products.cost) as Cost,
           (sum(sma_sale_items.net_unit_price)-sum(sma_products.cost))as profit
    From sma_sales, 
         sma_users, 
         sma_sale_items, 
         sma_products
    WHERE sma_sales.id = sma_sale_items.sale_id
      AND sma_sale_items.product_id = sma_products.id
      AND sma_sales.created_by = sma_users.id
    group by sma_sales.created_by, 
             cast(sma_sales.date as date);
";
$list = $this->db->query($queryStr)->result_array();