hod5783/d6.sql

11 lines
438 B
MySQL
Raw Normal View History

-- 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)
select c.*, count(o.customerid) c from sales as s
left join orders as o on (o.orderid = s.orderid)
left join customers as c on (c.customerid = o.customerid)
group by o.customerid
order by c desc
-- pick first one