//This is Visualforce Page
<apex:page Controller="TimeTaskController" sidebar="false" showHeader="false">
<apex:form >
<apex:pageBlock title="Time Details">
<apex:pageblockSection >
<apex:pageBlockTable value="{!timeList}" var="t">
<apex:column value="{!t.Role__c}"/>
<apex:column value="{!t.Rate__c}"/>
<apex:column value="{!t.Hour__c}"/>
</apex:pageBlockTable>
</apex:pageblockSection>
<apex:commandButton action="{!timeMergeRecords}" value="Merge"/>
</apex:pageBlock>
</apex:form>
</apex:page>
//------------------------------------------------------------------------------
// This is Controller.
public class TimeTaskController {
public List<Time__c> timeList{get;set;}
public List<Time__c> timeListAdmin;
public List<Time__c> timeListDeveloper;
Decimal adminHour;
Decimal devHour;
public TimeTaskController() {
timeList = new List<Time__c>();
timeListAdmin = new List<Time__c>();
timeListDeveloper = new List<Time__c>();
}
public void timeMergeRecords(){
adminHour=0;
devHour=0;
for(Integer i=0; i<=10 ; i++){
timeListAdmin = [Select id , Name , Role__c , Hour__c From Time__c where Role__c='Admin' AND Rate__c=:i ];
if(timeListAdmin.size()==1){
timeList.add(new Time__c(Role__c='Admin', Rate__c =i , Hour__c=timeListAdmin[0].Hour__c ));
}
else if(timeListAdmin.size()>1){
for(Time__c t : timeListAdmin){
adminHour += t.hour__c;
}
timeList.add(new Time__c(Role__c='Admin', Rate__c =i,Hour__c=adminHour ));
}
timeListDeveloper = [Select id , Name , Role__c , Hour__c From Time__c where Role__c='Developer' AND Rate__c=:i ];
if(timeListDeveloper.size()==1){
timeList.add(new Time__c(Role__c='Developer', Rate__c =i , Hour__c=timeListDeveloper[0].Hour__c ));
}
else if(timeListDeveloper.size()>1){
for(Time__c t : timeListDeveloper){
devHour += t.hour__c;
}
timeList.add(new Time__c(Role__c='Developer', Rate__c =i,Hour__c=devHour ));
}
}
}
}
// It's not best practice but i have done this task now you can also you map and done SOQl query without inside loop.