Question: I have one student object from that i am displaying values in visualforce page but i want to display number of males and females in that visualforce page i am not getting count of gender i am using picklist for gender, At the end of vf page i want to display male=10, female =10 for this may i know where i have to change code

Visualforce Page:

```
<apex:page controller="stu">
    <apex:form>
        <apex:pageblock>
            <apex:pageblockTable value="{!stulist}" var="s">
                <apex:column value="{!s.Student_Name__c}"/>
                <apex:column value="{!s.Contact_No__c}"/>
                <apex:column value="{!s.Gender__c}"/>
          </apex:pageblockTable>
      </apex:pageblock>
    </apex:form>
</apex:page>

```

Apex Class:

```
public class stu {

    public List<Students__c> find() {
        public List<Students__c> stulist = new List<Students__c>();
        public list<Students__c> getstulist(){
        stulist = [
            SELECT Student_Name__c, Contact_No__c,Gender__c
            FROM Students__c
        ];
        return stulist;
    }   
}

```

Answer: 
I would use an AggregateResult query to accomplish this. 

```
public class stu {

     public map<string,Id> genderCountMap         {get;set;}

     public stu(){
          AggregateResult[] results = [SELECT Count(Id) Quantity, Gender__c 
                                       FROM Contact 
                                       Where Gender__C != null 
                                       GROUP BY Gender__c
          ];
          for(AggregateResult ar : results){
            genderCountMap.put((string)ar.get('Gender__c'), (integer)ar.get('Quantity'));
          }

      }

       public List<Students__c> find() {
            public List<Students__c> stulist = new List<Students__c>();
            public list<Students__c> getstulist(){
            stulist = [
                 SELECT Student_Name__c, Contact_No__c,Gender__c
                 FROM Students__c
            ];
            return stulist;
       }  

       public Integer getMaleCount(){
            return genderCountMap.get('Male');
       } 
       public Integer getFemaleCount(){
            return genderCountMap.get('Female');
       }
}

```

Notice I added a property in the controller to hold the countMap, a constructor that uses an aggregate query to populate the map and a few functions to get the counts that you can use on your VF page

on your VF page, now all you need to do is

```
<apex:outputText value="{!MaleCount}"/>
<apex:outputText value="{!FemalCount}"/>

```

You can read a bit more on AggregateResults and their queries in some of these links

<http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm>

<http://blog.jeffdouglas.com/2010/04/12/using-aggregateresult-in-salesforce-com-soql/>
There are a number of different ways of doing this dependent upon what else is going on with the page. Perhaps the first thing to ask however is to note that there is a maximum list display size of 1000 records on a visualforce page - students sounds like a collection that could easily surpass 1000 records.

For counting you can either use the apex variable tag to create a counter for how many students are in a specific gender (say female), count these using the variable and then show the male count as the total list size minus the size of the counter variable (assuming the only options are male and female). Jeff Douglas has a good post on counter variables [here](http://blog.jeffdouglas.com/2010/04/02/visualforce-row-counter-for-iteration-components/).

The other option is to order the query in your controller by gender and then loop through the results and just have some custom counter variables that increment up (again you could probably count just a single gender if you were sure about the options).

The final way is to run a second query that uses the Count(fieldname) function with a Group By statement on Gender in a aggregate SOQL query then read off the results from the aggregate query and make them available for use on the page. The docs for aggregate queries are [available here](http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_select_count.htm).

Personally I would suggest using method 1 with counter variables, but it is useful for you to know all the available options as I imagine the page may do I more than just the snippet provided.