+ Start a Discussion

SOQL Query on Account Object

Hi Everyone,

I have 2 Custom fields as count__c and inte__c in Account Object. I want query for below credentials....

1) select count__c + inte__c from account;

2) select sum(count__c ) + sum(inte__c) from account;

How to achieve those things

Thanks & Best Regards,
Deepak Pandey 13Deepak Pandey 13
list<Account> lstacc =[select count__c , inte__c from account];
/* If you want to sum of these field */
integer adding = count__c + inte__c ;
I dont want to use in apex.I want to do it only with soql query in query editor
Deepak Pandey 13Deepak Pandey 13
i think it is not possible. i think you would create a formula field and query on this field.
bilal malikbilal malik
In SOQL queries, we cant do this using "+" operator.
we have to do this like
AggregateResult[] groupedResults = [Select Sum(count__c ) total_count,Sum(inte__c)total_intec from Account];
Decimal total_intec = (Decimal)groupedResults[0].get('total_intec');
 Decimal total_count = (Decimal)groupedResults[0].get('total_count');
 Decimal total_sum = total_count + total_intec;

Mark this as best answer if it helped you.
Ok Guys thanks for your suggestions.....
Suraj Tripathi 47Suraj Tripathi 47
You can use the aggregate queries for this.
AggregateResult[] aggregateData = [Select Sum(count__c ) countTotal,Sum(inte__c) intecTotal from Account]; 
Decimal countTotalValue = aggregateData[0].get('countTotal');
Decimal inteTotalValue = aggregateData[0].get('intecTotal');
Decimal totalSum = countTotalValue + inteTotalValue;

Points to remember--
1. Use aliases like 'countTotal' to get the value of the aggregate operation in the SOQL query.
2. to access the value from the aggregate result you have to use get() method as used above.

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.