2023-01-03 16:21:03 +00:00
|
|
|
-- had to fix column type for order_items/orderid from txt to int, else, was damned too slow
|
|
|
|
with sales as (select * from orders_items as oi
|
|
|
|
left join products as p on (oi.sku = p.sku)
|
|
|
|
where oi.unit_price < p.wholesale_cost)
|
2023-01-03 23:05:32 +00:00
|
|
|
select c.phone, count(o.customerid) c from sales as s
|
2023-01-03 16:21:03 +00:00
|
|
|
left join orders as o on (o.orderid = s.orderid)
|
|
|
|
left join customers as c on (c.customerid = o.customerid)
|
2023-01-03 23:05:32 +00:00
|
|
|
group by o.customerid, c.phone
|
2023-01-03 16:21:03 +00:00
|
|
|
order by c desc
|
2023-01-03 23:05:32 +00:00
|
|
|
limit 10
|
2023-01-03 16:21:03 +00:00
|
|
|
-- pick first one
|