SQL |
SELECT distinct (b.attr1) as origAttr1, max(CASE WHEN b.attr1_alias IS NULL OR RTRIM(LTRIM(b.attr1_alias)) = '' THEN b.attr1 ELSE b.attr1_alias END) as attr1, max(a.sku_id) as sku_id, max(b.attr1_entry_id) as attr1_entry_id, sum(e.qoh+e.qc) as qoh , max(b.attr1_order) as sort_order, max(c.attr1_hex) as hex,max(d.attr1_fam) as fam,max(a.SKU_OF16) as skuOF16,max(e.qoh) as maxqoh,max(e.qc) as maxqc,min(e.qoh) as minqoh,min(e.qc) as minqc,max(a.style_id) as style_id
FROM tb_skus a with (nolock)
inner join (select distinct sku_id as sku_id from TB_SKU_BUCKETS with (nolock) where 1=1 and store_id in (11,5,8,6,9,14,15,7,2,1,3,97) group by sku_id having sum(qoh+qc) >=4) skuqoh
on (skuqoh.sku_id=a.sku_id)
LEFT JOIN tb_attr1_entries b WITH (NOLOCK) ON (b.attr1_entry_id = a.attr1_entry_id)
LEFT JOIN tb_sku_buckets e WITH (NOLOCK) ON (e.sku_id = a.sku_id)
LEFT JOIN tb_attr1_hexes c WITH (NOLOCK) ON (b.attr1=c.attr1)
LEFT JOIN tb_attr1_families d WITH (NOLOCK) ON (c.attr1_fam_id = d.attr1_fam_id)
WHERE (a.sell_web not in ('N','NO') or (a.sell_web is null))
and a.style_id in (191608)
AND e.store_id in(11,5,8,6,9,14,15,7,2,1,3,97)
GROUP BY b.attr1
HAVING sum(e.qoh+e.qc) >= 4
ORDER BY max(b.attr1_order), max(b.attr1_entry_id) |