function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jasjeet GrewalJasjeet Grewal 

wrapper class join two soql queries

Hi all,

I have two objects Account and Sales. Account has fields created date, location, down payment received. Sales has fields date, location, total revenue.

What i am trying to do is to compare sum(down payment received) group by created date and location with total revenue from sales object.

I have made couple of queries wriiten below. I want to use these queries for wrapper class. My main concern now is somehow I want to link both these queries together, so I can view it as table on vf page. 
 
From Account: 
select sum(down_payment_received__c) from account where created_date__c > 2018-03-12T00:00:00Z and created_date__c < 2018-03-13T00:00:00Z and Location__c in ('Location A', 'Location B', 'Location C', 'Location D') group by Location__c


This query will give me result for Mar 12 and for all Locations. 

Similarly, For Sales object:
1select Date__c, Location__c, Total_Revenue_End_of_the_day__c from sales__c where Date__c = 2018-03-12

This query will give results for sales object. 

But, what I wanted is use a single query to get results from both objects.

I would like a data such as following (layout could be little different)

Date                     Location            Down Payment            Total Revenue             Total Revenue - Down Payment
2018-03-18           Location A                 100                              100                                           0
2018-03-18           Location B                 150                              100                                          50


Please, let me know how this could be possible. 
Vinit JoganiVinit Jogani
Here Location is kind of Master object. Account have lookup to Location and Sales have lookup to Location. Account and Sales are not directly related. Neither they are in same hierarchy: child - parent - grandparent. I dont think you will be able to query the required data in one SOQL.

You can try and optimize the queries though. Take out unique location ids from first query and use it in 2nd to further filter the data.
Jasjeet GrewalJasjeet Grewal
Hi Vinit,

Location here is just a string value from picklist.

If join isnt possible, is there a way I can use list<list> or map to store all data and later use it to display data in vf page?
Vinit JoganiVinit Jogani
Hi Jasjeet,

My bad, I misunderstood it. In that case you have to do separate query. Create a wrapper object will stores all the data for each location. Create a Map <Location, WrapperObject> to store result of first query and use this map to populate data from send query. This is what I can think of right now.