แก้ให้ตรงกับเงื่อนไขของตาราง pttype_spp ครับ
*****
set @ds1:='2012-10-1';
set @ds2:='2012-10-1';
select lo.lab_items_code code,lo.lab_items_name_ref name
,count(*) 'Total'
,ifnull(sum(p.pcode in ('A2')),0) 'A2'
,ifnull(sum(p.pcode in ('A7')),0) 'A7'
,ifnull(sum(p.pcode in ('AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','UC')),0) 'UC'
,ifnull(sum(p.pcode in ('AL')),0) as 'AL'
,ifnull(sum(p.pcode not in ('A2','A7','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','UC') or p.pcode is null),0) 'Other'
from lab_head lh
left join lab_order lo on lo.lab_order_number=lh.lab_order_number
left join ovst o on o.vn=lh.vn
left join pttype p on p.pttype=o.pttype
where lh.order_date between @ds1 and @ds2
group by lo.lab_items_code
order by name