set @count_x = 0;
set @count_xx = 0;
set @pid_cc = 0;
select p.hn,p.pname,p.fname,p.lname,o.vstdate
,@count_x:=@count_x+1 as 'นับรวม'
,if(p.hn = @pid_cc,@count_xx:=@count_xx+1,@count_xx:=1) as 'นับรวมรายHN'
,@pid_cc:= o.hn as check_id
from patient p
left outer join ovst o on o.hn=p.hn
where year(o.vstdate)='2017'
order by p.hn