...ลองแบบนี้น่าจะพอได้นะครับ....
select a.main_pdx,count(a.main_pdx) as pdx_count ,i.name as icdname
from vn_stat a
join ovst v on v.vn=a.vn
left outer join icd101 i on i.code=a.main_pdx and v.cur_dep="012"
where a.vstdate between'2008-09-01' and '2008-09-30'
and (v.vsttime between '20:30:00' and '23:59:59' or v.vsttime between '00:00:00' and '08:29:59')
and (a.main_pdx<>'' or a.main_pdx is not null)
group by a.main_pdx,i.name
order by pdx_count desc
limit 20
....
....