select t1rp2 as g0, sum(t1rp3)/sum(t1rp4) as p0, sum( case when t1rp5 > 1 then 1 when t2rp1 > 20200 then 2 when t1rp6 > 15 then 3 when t3rp1 > 150 then 4 else 5 end ) as p1 from ProductView p left outer join ( select avg(a.address_valuation) as t1rp1, sum(a.address_zone) as t1rp2, sum(a.address_zone) as t1rp3, count(a.address_zone) as t1rp4, avg(o.order_serverId) as t1rp5, avg(c.customer_balance) as t1rp6, p.product_id as t1pk from ProductView p left outer join OrderItemView oi on p.product_id = oi.orderItem_productId left outer join OrderView o on oi.orderItem_orderId = o.order_id left outer join AddressView a on o.order_customerId = a.address_customerId left outer join CustomerView c on o.order_customerId = c.customer_id group by p.product_id ) t1 on p.product_id = t1pk left outer join ( select min(a.address_zip) as t2rp1, p.product_id as t2pk from ProductView p left outer join OrderItemView oi on p.product_id = oi.orderItem_productId left outer join OrderView o on oi.orderItem_orderId = o.order_id left outer join AddressView a on o.order_customerId = a.address_customerId where a.address_state in ('PA', 'CA', 'VA', 'MA', 'ME', 'MD', 'CO', 'MO') group by p.product_id ) t2 on p.product_id = t2pk left outer join ( select ca.category_warehouseSqft as t3rp1, p.product_id as t3pk from ProductView p left outer join CategoryView ca on p.product_categoryName = ca.category_name where ca.category_seasonal = true ) t3 on p.product_id = t3pk where t1rp1 > 10000.0 group by t1rp2 order by p0 limit 500;