You would want to use an AggregateResult query here. This is obviously just some shorthand code as I really don't know your object or field names so you will have to adjust those to fit in order for this to compile for you
AggregateResult[] ARs
= [SELECT count(Id) myCount, Cabin_Type__c, Deck_Type__c
FROM cruise_ deck_Cabin_paln__c
GROUP BY Cabin_Type__c, Deck_Type__c];
This will return a a list of AggregateResults objects. Each aggregate result object retruned by this query will look something like this
{myCount=2, Cabin_Type__c=Twin, Deck_Type__c=2}
Depending on how you plan to use the results would affect how I would suggest manipulating them. I often create a wrapperclass. If you wanted to use a wrapper class here it would have 3 properties, Deck, Cabin Type, and Quantity. It would look something like this
public class myWrapper {
public Integer Quantity { get; private set; }
public String Deck { get; private set; }
public String CabinType { get; private set; }
public myWrapper(AggregateResult ar) {
Quantity = (Integer) ar.get('Quantity');
Deck = (String) ar.get('Deck_Type__c');
cabinType = (String) ar.get('Cabin_Type__c');
}
}
And you could populate it like so
List<myWrapper> wraps = new List<myWrapper>();
for (AggregateResult ar : ARs) {
wraps.add(new myWrapper(ar));
}
But for purposes of just being able to easily access the data, you could just populate a map with the results like so
map<string,integer> countMap = new map<string,integer>();
for(AggregateResult ar : ARs){
countMap.put((string) ar.get('Cabin_Type__c') + ',' + (string) ar.get('Deck_Type__c'), (integer) ar.get('myCount'));
}
You now have a map where the key is the concatenation of the Deck and Cabin type, and the value it returns is the count.
Use COUNT(Id) to get a record count per category.
To order the results, use ORDER BY Category__c DESC.
You can't easily return a count of distinct categories returned, but really all you need here is to count the number of rows returned.
Best Answer
If you aren't concerned about having this information available in code, you can get this information by going to the setup menu -> System Overview (3rd link from the top on the left-side menu) -> click on the number listed under 'Data Storage'
For url-hackers, it's
salesforce.com/setup/org/orgstorageusage.jsp?id=<your orgId>
Under the 'Data Storage' header, this page shows you a count of how many records of each object (standard and custom) the org contains, as well as the amount of storage space each object is consuming, sorted by storage space used