SELECT DISTINCT
villageschool.schoolname as 'โรงเรียน',

SUM(case WHEN classeducate=0 THEN 1 else 0 end) as 'ก่อนอนุบาล',
SUM(case WHEN classeducate=1 THEN 1 else 0 end) as 'อนุบาล 1',
SUM(case WHEN classeducate=2 THEN 1 else 0 end) as 'อนุบาล 2',
SUM(case WHEN classeducate=3 THEN 1 else 0 end) as 'อนุบาล 3',
SUM(case WHEN classeducate=4 THEN 1 else 0 end) as 'ประถม 1',
SUM(case WHEN classeducate=5 THEN 1 else 0 end) as 'ประถม 2',
SUM(case WHEN classeducate=6 THEN 1 else 0 end) as 'ประถม 3',
SUM(case WHEN classeducate=7 THEN 1 else 0 end) as 'ประถม 4',
SUM(case WHEN classeducate=8 THEN 1 else 0 end) as 'ประถม 5',
SUM(case WHEN classeducate=9 THEN 1 else 0 end) as 'ประถม 6',
SUM(case WHEN classeducate=10 THEN 1 else 0 end) as 'มัธยม 1',
SUM(case WHEN classeducate=11 THEN 1 else 0 end) as 'มัธยม 2',
SUM(case WHEN classeducate=12 THEN 1 else 0 end) as 'มัธยม 3',
SUM(case WHEN classeducate=13 THEN 1 else 0 end) as 'มัธยม 4',
SUM(case WHEN classeducate=14 THEN 1 else 0 end) as'มัธยม 5',
SUM(case WHEN classeducate=15 THEN 1 else 0 end) as'มัธยม 6'

FROM
personstudent
INNER JOIN villageschool ON personstudent.pcucodeperson = villageschool.pcucode AND personstudent.villcode = villageschool.villcode
GROUP BY villageschool.schoolname;