0

I am trying to group the number of times a subscriber has visited each unique store but am getting an error:

select subscriberkey, storename, count (orderID) as shopvisits from salesorder 
   group by subscriberkey,storename

What is missing from/incorrect about this query?

Thanks

1 Answer 1

0

SOQL doesn't support 'as' keyword in aliasing. You can alias a table like

select o.subscriberykey, o.storname, o.orderid from salesorder o;

Also you should use Aggregate Functions in your case. Aggregate functions in SOQL, such as AVG(), SUM() and MAX(), allow you to roll up and summarize your data in a query.

AggregateResult[] groupedResults = [SELECT AVG(Amount)aver FROM Salesorder];
Object avgAmount = groupedResults[0].get('aver');

Note that any query that includes an aggregate function returns its results in an array of AggregateResult objects. AggregateResult is a read-only sObject and is only used for query results.

An example of Aggregate Function with a Group By clause. This is not the exact solution you are looking for as I don't know your data model. This is just for representation of an aggregate function which returns AggregateResult

AggregateResult result:[SELECT status__c status, Count(Id) Sum, 
                               subscriber__c subscriber 
                          FROM Salesorder 
                         WHERE status__c IN ('Fulfilled', 'Received') 
                      GROUP BY status__c, subscriber__C];
2
  • How would I write that for this: SELECT subscriberkey, Count(orderid) shopvisits, storename FROM Salesorder GROUP BY subscriberkey, storename When I check syntax I get a notification indicating that Select must be the first word of the query. @bennie
    – Olivia Dam
    Commented Jan 10, 2016 at 17:40
  • Did you paste the query in with a bunch of leading spaces? Remove them if so.
    – Adrian Larson
    Commented Jan 10, 2016 at 22:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .