How to create an SQL report
The Advanced Reports extension for Magento 2 can build a report from an SQL statement you write yourself. Use it for a question the Report builder can't answer. The statement is read-only, and the report works like any other: readers can pick a period or store view, sort, filter, chart and export it, and send it by email.
Before you start
- Set Enable SQL reports to
Yesin the settings. - Your admin role needs both Authoring SQL Reports and Changing Reports (see How to restrict access to a specific report).
1. Write the statement
Go to Reports -> Advanced Reports -> Reports and click New SQL Report. A new report opens with the Query card on the left and Report settings on the right. Enter a Title under General Information, and the statement in the SQL query field.
This example shows orders and revenue by month and customer group:
SELECT DATE_FORMAT(o.created_at, '%Y-%m') AS order_month,
g.customer_group_code AS group_code,
COUNT(*) AS orders,
SUM(o.base_grand_total) AS revenue
FROM {{sales_order}} AS o
LEFT JOIN {{customer_group}} AS g ON g.customer_group_id = o.customer_group_id
WHERE o.state <> 'canceled'
{% if period %} AND o.created_at BETWEEN {{period}} {% endif %}
{% if store_id %} AND o.store_id = {{store_id}} {% endif %}
GROUP BY order_month, group_code
Click Run query to try it. The table and chart show the result, and the line under the query says how long it took and how many rows came back. Nothing is saved until you click Save.
Table names
Write each table as {{table_name}}, without the database table prefix: the extension adds the store's prefix itself, so the report also works on a store whose tables carry one. A table written directly, without braces, is read exactly as written, and the editor warns This query will not travel.
What a statement may contain
A statement must be a single SELECT, or WITH ... SELECT. Comments are allowed. The extension refuses anything that could change data or the database, such as INSERT, UPDATE, DELETE, DROP, CREATE, ALTER, SET, INTO OUTFILE or SLEEP. These words are refused anywhere outside quotes and comments, so a column alias such as check or start must be written in backticks.
A statement runs for at most Stop a query after (seconds) from the settings, 30 seconds by default. If a report hits the limit, narrow the period or the filters.
2. Add parameters
A parameter is a value the reader picks on the report's toolbar. Write it in the statement as {{name}}, where name is not the name of a table. The value is passed to the database separately and is never written into the statement.
After Run query, each parameter appears under Parameters in Report settings, where you set:
- the type: Text, Number, Date, Date range, Select or Multiple Select;
- Label (optional): the name shown on the toolbar;
- Required: whether the report runs without a value;
- Default: the value the report opens with. A Date range offers periods such as Last 30 days or Month to date, worked out again each time the report is read.
A Select or Multiple Select parameter lists its options, each with a value and a label; press Add option to add one. A parameter named store_id is filled in with the store views by itself.
How a parameter is written depends on its type:
- Date range:
created_at BETWEEN {{period}}covers the whole period, from the start of its first day to the end of its last.{{period.from}}and{{period.to}}give each end on its own. - Multiple Select:
status IN ({{statuses}}). - Any other type:
= {{name}},> {{name}}and so on.
A parameter that is not required may be left empty. Wrap the part of the statement that uses it in {% if name %} ... {% endif %}, as the example does: the part is left out while the parameter is empty, so the report reads every period or every store view.
3. Set up the columns
Under Columns, each column of the result has a Label, which renames its header, and a type: Auto, Text, Number, Money, Quantity, Percent or Date. Auto reads the type from the values that came back. In the example, set revenue to Money.
Number, money, quantity and percent columns are summed in the totals row, and can be drawn on the chart; text and date columns describe the rows.
The totals row of an SQL report adds up the rows on the current page.
Use the Sorting and Filters sections to set the order and the conditions the report opens with, rather than an ORDER BY in the statement. Visualization sets up the chart.
4. Save the report
Click Save. The report appears on the Reports list under Custom, and under the SQL chip with the other SQL reports. If you change a parameter on the toolbar before saving, the value you picked becomes its default, unless you also changed that default under Parameters.
Readers who may open the report but not write SQL see the result, the toolbar parameters, sorting and filters, but not the statement.
When Enable SQL reports is set back to No, SQL reports stay listed but cannot run, and their statements cannot be changed.