select cright.rightcode,cright.rightname,
count(person.pid) as 'จำนวนสิทธิทั้งหมด',
count(case when person.dateexpire < current_date() then person.pid else null end) as 'สิทธิหมดอายุ'
from person
left join cright on person.rightcode = cright.rightcode
group by cright.rightname
order by cright.rightcode ;