You can use the following technique to achieve this, note that it only works when you know the total variations in your case its just sub1 and sub2 , if there are more you need to add them in the query or generate a complete dynamic query
select name, max(case when subject='sub1' then attendance end ) as 'sub1', max(case when subject='sub2' then attendance end ) as 'sub2' from test group by name
DEMO
To make it dynamic its a bit complicated, google it about mysql dynamic pivoting and you will get some tutorial to understand how it works.
For your case you can have like below -
set @d_sql = null; select group_concat(distinct concat( 'max(case when subject = ''', subject, ''' then attendance else null end) as ', concat('`',subject, '`') ) ) into @d_sql from test; set @d_sql = concat('SELECT name,', @d_sql, ' from test group by name'); PREPARE stmt from @d_sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
DEMO