I am currently trying to perform a query with Laravel / Eloquent but I decided to write the SQL query first in order to figure out what the problem is. I want to group by dates and combine the results of two tables using multiple joins but the results get wrong when I add the product_metrics table like this:
inner join `product_metrics` on `products`.`id` = `product_metrics`.`product_id`
select
date_format(`date` + INTERVAL 1 HOUR, '%Y-%m-%d') as day,
round(sum(`item_price` * `quantity_ordered`), 2) as sales,
sum(`quantity_ordered`) as units,
sum(
CASE
WHEN `is_refund` THEN `item_price` * `quantity_ordered`
ELSE 0
END
) as refunds,
sum(
CASE
WHEN `is_refund` THEN `quantity_ordered`
ELSE 0
END
) as refund_units,
sum(`promotion_discount`) as discounts,
sum(`sessions`) as sessions,
count(DISTINCT(customer_order_id)) as orders,
round(sum(`product_sales`), 2) as sales_sc,
round(
sum(`unit_session_percentage` * `product_sales`) / sum(`product_sales`),
2
) as cvr,
sum(`page_views`) as views
from
`products`
inner join `product_brands` on `products`.`brand_id` = `product_brands`.`id`
inner join `customer_order_items` on `products`.`id` = `customer_order_items`.`product_id`
inner join `customer_orders` on `customer_order_items`.`customer_order_id` = `customer_orders`.`id`
inner join `product_metrics` on `products`.`id` = `product_metrics`.`product_id`
inner join `customers` on `products`.`customer_id` = `customers`.`id`
inner join `marketplaces` on `products`.`marketplace_id` = `marketplaces`.`id`
where
(
`date` between "2019-05-01 00:00:00"
and "2019-05-31 00:00:00"
or `purchase_date` between "2019-05-01 00:00:00"
and "2019-05-31 00:00:00"
and `customers`.`id` = 18
)
and `products`.`deleted_at` is null
group by
date_format(`date` + INTERVAL 1 HOUR, '%Y-%m-%d')
order by
`day` asc
What I want / need to do is: Query for products with a where clause and also add dates to get all product_metrics and all customer_orders related to those products in the given period of time.
via Chebli Mohamed

Aucun commentaire:
Enregistrer un commentaire