declare @T table
(
Gender varchar(10),
Age int,
Name varchar(10)
)
insert into @T values ('Male', 30, 'Bill')
insert into @T values ('Female', 27, 'Jenny')
insert into @T values ('Female', 27, 'Debby')
insert into @T values ('Male', 44, 'Frank')
select Gender as 'li',
(select T2.Age as 'li',
(select T3.Name as 'li'
from @T as T3
where T2.Age = T3.Age and
T1.Gender = T3.Gender
for xml path(''), root('ul'), type) as 'li'
from @T as T2
where T1.Gender = T2.Gender
group by T2.Age
for xml path(''), root('ul'), type) as 'li'
from @T as T1
group by Gender
for xml path('ul')