Merged Results is a post-query processing feature that, if not used thoughtfully, can overtax Looker instance resources and cause your Looker instance to respond more slowly for all users. Best practice is to define functions and logic in LookML instead, which generates SQL that is processed by your database. View the Best practice: Optimize Looker performance Help Center article for more information about optimizing Looker performance.
Explores in Looker are designed by your Looker developers to combine the data from your database tables in the most effective way by using defined relationships between data fields and tables. Because of this, it is best to use a single Explore to examine your data.
However, there may be times when your Looker developers haven’t created the relationships you need or have encountered technical limitations. In these cases, Merged Results lets you combine results from different Explores (even from different models or projects) to create data tables on which to Explore and create visualizations. You can use Merged Results as a proof of concept to further develop and define your LookML projects and models.
Understanding merged results
When you merge queries, you start out by creating a single query from a single Explore, and then you combine other queries with that first query.
The Merged Results feature has a limit of 5,000 rows of data for each of the merged queries. If you include queries that return more than 5,000 rows of data, only the first 5,000 rows returned are included in the merged results.
By default, that first query is considered the primary query. This is an important concept because when Looker matches the data to create the merged results, it matches each added query to the primary query (not to any other added query). So, whenever you add a query, you need to include a dimension that can be matched to a dimension in the primary query.
You can set an added query as the primary query by switching the primary query.
Below is an example of a set of queries and their merged results:
All of the primary query’s fields are displayed in the merged results, using the primary query’s names for the fields. This means that if the primary query and an added query use different names for a matching dimension, only the primary query’s dimension name will be displayed in the results.
Merged results do not perform an actual SQL join. But, for those who are familiar with SQL joins, the Merged Results feature combines the results of multiple Explores in a similar way as would a left join. The results of the added query are combined with the results of the primary query as if they are being left joined into the primary query.
If you aren’t familiar with the idea of a left join, no worries. In practical terms, here’s why it matters which query is the primary query:
- How field names appear: For matching fields, the primary query’s field names are used in the merged results, as shown above.
- How merged results handle a query without a matching value: This section below discusses how Looker handles merging data when only some of the queries have specific value(s) in the matching dimension(s).
- How merged results handle a query having multiple matching values: This section below discusses how Looker handles merging data when some of the queries have multiple rows with a specific value (or combination of values) in the matching dimension(s).
What if one query doesn’t have a matching data value?
Another reason the primary query is important is because of the way null values are handled in the matched dimensions:
- If a row exists in the primary query but not in the additional query, then the added query’s fields will be NULL for that row.
In the example shown below, the primary query has a row for Maternity, so the merged results show this row. The added query does not have a Maternity row, so any of the fields from the added query will show NULL for Maternity.
- If a row exists in the added query but not in the primary query, then the row will not show in the results at all.
In the example below, the added query has two rows for Jeans, but the primary query does not. So this row is not shown in the merged results at all.
In our example, if we switch the primary query to make the added query as the new primary query, we get the merged results shown below.
Note that now we don’t have the Maternity rows because they do not exist in our new primary query. However, now we do have the Jeans rows and those rows show NULL for the dimensions and measures that are only in the query that is added to the primary query.
What if one query has multiple rows for the same value?
Finally, designating the desired primary query is also important because of the way multiple rows with matching values are handled. If the added query has two or more rows with values that match a row in the primary query, the primary query row will be duplicated that number of times.
In the example below, the added query has two rows for Dresses. In the merged results, the Dresses values from the primary query appear twice, once for each of the Dresses rows from the added query:
Note that if you switch the primary query in this case, you would still have two Dresses rows, since the newly designated primary query has two rows for Dresses. The takeaway is that, when you merge queries, the results may have more rows than the primary query has — but there will never be fewer rows.
To merge the results from multiple queries, you need to complete these steps:
- Create the first source query, called the primary query.
- Add the next source query.
- Check the merge rules for those queries and run the merge.
- Sort, pivot, and create visualizations for the results.
- Reuse and share the results using the URL.
- Modify the results by editing the source queries or adding source queries.
Creating the primary query
To merge the results from multiple queries, you start with a beginning query, which is considered the primary query:
- Select an Explore from the Explore menu.
- Select the dimensions and measures of interest from the field picker. Do not pivot any dimensions during this step.
This is all you need to start merging results. However, you can also use some advanced exploring techniques to further refine your query. You can:
- Optionally, add filters for the data.
- Optionally, include table calculations to create ad hoc metrics.
- Optionally, click Run to see the results of your primary query and to test your filters and table calculations.
Adding the next source query
Once you’ve created your primary Explore, you can add another source query:
- In your Explore, click the gear icon.
- Select Merge Results. This will open the Choose an Explore window.
- In the Choose an Explore window, click on the name of an Explore where you will create your next query.
Looker opens the Explore in the Edit Query window, where you can build the new query to be merged into your primary query:
To merge queries, Looker finds dimensions in the queries whose values can be matched. Be sure that your queries contain at least one common dimension whose values can be matched exactly. For example, if both queries have a Date dimension, but one query uses “2017-10-01” as a value and the other query uses “October 2017” as a value, Looker can’t use that dimension to merge the queries.
- Select the dimensions and measures of interest from the field picker. Be sure to include at least one dimension that will exactly match a dimension in the primary query. Do not pivot any dimensions during this step.
- Optionally, include filters to narrow the data.
- Optionally, incorporate table calculations to create new fields based on the query fields.
- Optionally, click Run to see the results of the source query and to test your filters and table calculations.
- Click Save to merge the query into your primary query.
Checking the merge rules and running the merge
Looker automatically finds the best dimensions to use for matching the queries and displays these matches in the Merge Rules section. Looker shows how each query will be merged with the primary query:
Review the dimensions that Looker used to match the queries. (See Editing Merge Rules for information on changing these rules.)
Click Run to see the merged query results:
Note that any table calculations from the source queries are displayed as standard dimensions in the merged results.
Using and modifying the merged results
You can use the merged results to:
- Examine and sort the data, including drilling into the data. If you drill into the dimension used to merge the two Explores, however, Looker displays only drill fields from the primary Explore.
- View totals. Looker calculates totals on each of the component queries and uses those totals in the merged results. Therefore, totals may appear too high, because what you are seeing are totals calculated before the results were merged. One way to avoid this is to align the filters on each query.
- Create visualizations.
- Pivot dimensions in the merged results by selecting Pivot from the gear menu in the dimension’s column of the data table. Note that you can’t pivot dimensions in the source queries.
To reuse the merged results, you can:
- Share the results using the browser URL.
- Bookmark the URL in your browser to run the same merged query again in the future. You can’t save the merged results as a Look.
- Save the merged results as a tile on a dashboard (see Saving Your Merged Results to a Dashboard).
If you want to modify the merged results, you can:
- Merge queries from additional Explores by clicking the Add Query button and following the same steps.
- Edit the source queries or configure the way the queries are merged. See Editing Merged Results for more information.
- Create filters by adding those filters in the source queries, either when creating and merging the queries, or by editing the queries from the Merged Results page. Note that you can’t add a filter directly to the merged results.
If you want to clear the cache and retrieve fresh results from your database, select the Clear Cache & Refresh option from the gear menu at the top right of the Merged Results window.
To download your merged results query, you can save the query to a dashboard and then download the dashboard as a PDF or as a collection of CSV files.
Editing merged results
Once you have your merged results, you still have the flexibility to edit the merged queries and the rules used to merge the queries:
Editing the source queries
From the Merged Results window, you can go back and edit the source queries by clicking on the query name in the left pane, or by selecting Edit from the query’s gear menu. These options take you back to the Edit Query screen:
From here you can add or remove fields, add table calculations, or change the source query’s filters. Click Save to return to your merged results.
The query’s gear menu also has these options:
- Rename: Specify a different name to display for the query in the Merged Results window.
- Make Primary: Make the query the base for the merged results. See Understanding Merged Results to understand the role of the primary query.
- Delete: Remove the query from the merged results. (If you want to add the query back into the merged results after you’ve deleted it, you can use the Back button in your browser.)
Editing the merge rules
When you add a query, Looker automatically finds dimensions it can use to match the added query to the primary query. Each added query must have at least one dimension whose values exactly match up to a dimension in the primary query. Looker displays these matches in the Merge Rules section, showing how each added query will be merged with the primary query:
You can use the Merge Rules section to change or add the dimensions used for matching:
- Use the drop-down menu to see other dimension options for matching the data.
- If there are additional dimensions that could be used for matching, Looker displays + Add dimension. Click + Add dimension to configure an additional set of dimensions to use in the query merge.
- Click on the X if you don’t want to match the data between the two dimensions.
Switching the primary query
When merging queries, we start out by creating a single query from a single Explore, and then we add other queries by combining them with that first query. By default, that first query is considered the primary query, but we can designate any query as the primary query by selecting Make Primary from the query’s gear menu:
Each added query must have at least one dimension whose values can be matched exactly to a dimension in the primary query.
When you switch the primary query, the merged results are likely to change. See Understanding Merged Results to understand the role of the primary query.
Saving your merged results to a dashboard
Once you’ve added your merged results query to a dashboard, you can add or apply dashboard filters to your merged results tile, rearrange the tile, edit the tile, or add new tiles to your dashboard. You can’t download the data from a tile based on merged results, but you can download the dashboard as a PDF or as a collection of CSV files.
Any dashboard filters applied to a merged results tile will be turned off if the merged query is changed in any way. You can reinstate the filters by turning them back on again in the filter configuration window.
You can also use a merged results query on a LookML dashboard by first saving the query to a user-defined dashboard and then converting that dashboard to LookML. To convert a user-defined dashboard to LookML, follow the steps outlined on the Building LookML dashboards documentation page.
Merging queries in embedded Looks, dashboards, and Explores
- Hover over the Look’s title to reveal the Look’s gear menu and click on the gear menu.
- Select Merge Results, then follow the steps for merging queries.
To merge an embedded dashboard tile’s query:
- Hover over the dashboard tile to review the drop-down menu for the dashboard tile.
- From the drop-down menu, choose Explore From Here. Looker opens the Explore for that tile’s query.
- Click on the Explore’s gear menu and select Merge Results.
- Follow the steps for merging queries.
Whenever possible, you should use the data from a single Explore because your Looker developers have carefully considered how the data from different database tables should be combined. When needed, though, merging results is a powerful technique that lets you combine data from multiple Explores and databases.