I am trying to run this query in apex and run into following error.

        AggregateResult[] groupedResults = [select Campaign__r.id cid, Count(opportunity__c) oppcount, FORMAT(SUM(Opportunity__r.Amount)) oppAmt from Campaign_influence__c where opportunity__c!=null AND Campaign__c in :CampaignList group by Campaign__r.id ];

> System.QueryException: Invalid field: 'Opportunity__r': 

This query does run fine in developer console 'query editor'. It also works if I replace `FORMAT(SUM(Opportunity__r.Amount))` with just `SUM(Opportunity__r.amount)`. What am I doing wrong here?