SOQL GROUP BY ROLLUP Clause

Download Free Salesforce Training Materials with Realtime Scenarios

SOQL GROUP BY ROLLUP Clause

In our previous Salesforce Tutorial we have learned about some advanced SOQL statements in salesforce. In this salesforce Training Tutorial we are going to learn about SOQL GROUP BY ROLLUP Clause, FOR REFERENCE Clause and FOR VIEW Clause.

What is SOQL GROUP BY ROLLUP Clause ?

SOQL GROUP BY ROLLUP Clause is used to add subtotals to get aggregates data in the query results. Both GROUP BY and GROUP BY ROLLUP Clause are similar but SOQL  GROUP BY ROLLUP Clause returns multiple levels of subtotal rows. When using  GROUP BY ROLLUP Clause in a statement we can add up to three fields in a comma-separated list.

  • SELECT city_c, State_c, COUNT(Employee_Name__C) Counts FROM Employee__C GROUP BY ROLLUP (City__c, State__C).
SOQL GROUP BY ROLLUP Clause

SOQL GROUP BY ROLLUP Clause

From above SOQL statement Grouped the data according to State, city and also w calculates the number.

SOQL FOR REFERENCE Clause

This SOQL FOR REFERENCE clause is used to find Date or Time values when a record has been referenced. The LastReferencedDate field is automatically updated for any retrieved records.

When coming to FOR REFERENCE Clause, this is used to tack Date an Time of the record when a record has been referenced last while executing a SOQL query.

  • SELECT city__c, state__c, lastreferenceddate FROM Employee__C FOR REFERENCE

When we execute the above SOQL statement all the records will show the same date and time .

SOQL FOR VIEW Clause.

FOR VIEW Clause is used to find the date of the record when it has been last viewed. Every record will have a standard field called LastViewedDate. This field will be updated for every retrieved records.

FOR VIEW clause is used to track the date when the records was viewed last while executing a SOQL Query.

  • SELECT City_c, State_c, lastViewedDate FROM Employee__C FOR VIEW

When the above statement is executed for first time it shows the last viewed date and when the SOQL statement is executed for second time all the records will show the same date and time for lastVieweddate.