SELECT
visitdiag.pcucode,
visitdiag.dateupdate,
visitdiag.dxtype,
visitdiag.diagcode,
visit.pid,
person.prename,
person.fname,
person.lname,
person.pid,
person.sex,
person.birth,
person.hnomoi,
person.mumoi,
person.subdistcodemoi,
person.dischargetype,
person.typelive
FROM
visitdiag
INNER JOIN visit ON visitdiag.pcucode = visit.pcucode AND visitdiag.visitno = visit.visitno
INNER JOIN person ON visit.pcucode = person.pcucodeperson AND visit.pid = person.pid
WHERE  visit.visitdate BETWEEN 20131001 and 20140331
and visitdiag.diagcode = 'z12.3'
GROUP BY visitdiag.visitno
ORDER BY
visit.visitdate ASC;