Report with a custom SQL query

Top  Download  Previous  Next

You will create a summary of how often roles occur in user groups – in total and broken down into activated and deleted accounts. The report has two multiple-selection parameters: group and role.

1.Select the report type and the data source:

•In the report designer, select Tabular report.

•Go to the data source settings.

•Select Custom SQL query.

en_rep_designer-sql_custom-sql-query_01_zoom16

2.Insert the SQL query in the editor:

en_rep_designer-sql_sql-editor_01_zoom16

WITH all_users AS (

SELECT

report.users.id as id,

is_activated,

is_deleted,

report.user_groups.id as group_id,

report.user_groups.name as group_name,

report.users.user_helpdesk_role_id as role_id,

CASE

WHEN report.users.user_helpdesk_role_id = 0 THEN 'user'

WHEN report.users.user_helpdesk_role_id = 1 THEN 'staff'

WHEN report.users.user_helpdesk_role_id = 2 THEN 'admin'

END as role_name

FROM report.users

JOIN report.users_user_groups

ON report.users.id = report.users_user_groups.user_id

JOIN report.user_groups

ON report.users_user_groups.user_group_id = report.user_groups.id

)

SELECT

group_id,

group_name,

role_name,

role_id,

COUNT(id) AS total,

SUM(CASE WHEN is_deleted = TRUE THEN 1 ELSE 0 END) AS deleted_count,

SUM(CASE WHEN is_activated = TRUE THEN 1 ELSE 0 END) AS activated_count

FROM all_users

GROUP BY group_id, group_name, role_name, role_id

3.Select the report fields: displayed, grouping and summary fields.

en_rep_designer-sql_report-fields_01_zoom16

4.Fill in the report title, save the changes and check the report preview.

en_rep_designer-sql_preview-basic_01_zoom18

5.Prepare data for the selection lists in the parameters: in the side pane, add a dataset with the user_groups and user_helpdesk_roles tables.

en_rep_designer-sql_dataset-lookup_01_zoom16

en_rep_designer-sql_dataset-lookup_02_zoom16

6.Add two report parameters – group and role – with the options: multiple selection, no null value, no default value.

en_rep_designer-sql_parameters-multiselect_01_zoom16

en_rep_designer-sql_parameters-multiselect_02_zoom16

7.Add conditions to the basic SQL query (after the last JOIN expression):

WHERE (

@role_ids = ''

OR

report.users.user_helpdesk_role_id = any(string_to_array(@role_ids, ',')::int[])

) AND (

@group_ids = ''

OR

report.user_groups.id = any(string_to_array(@group_ids, ',')::int[])

)

In the conditions:

•@role_ids and @group_ids are the values of the query parameters

•An empty parameter means that all values are selected

•The string_to_array function splits the values into a list of numbers

•The any() function checks for a match with the list

8.In the Data Source Wizard window, create the query parameters:

•role_ids – type Expression; result: text created by joining the values of the RoleId report parameter (join function)

•group_ids – in the same way

en_rep_designer-sql_query-parameter-role-ids_01_zoom16

en_rep_designer-sql_query-parameter-group-ids_01_zoom16

9.Adjust the appearance: change the column names, colors and fonts.

en_rep_designer-sql_layout-columns_01_zoom16

en_rep_designer-sql_layout-colors-fonts_01_zoom16

10.Correct the summary fields: remove unnecessary values and change the format to a readable one. Check the result.

en_rep_designer-sql_summary-fields_01_zoom16

en_rep_designer-sql_preview-summary_01_zoom18

en_rep_designer-sql_preview-summary_02_zoom17

11.Add rows for the parameter values: right-click on a table cell and add rows from the context menu.

en_rep_designer-sql_parameter-rows-add_01_zoom16

12.Enter the parameter names and align the font formatting.

en_rep_designer-sql_parameter-names_01_zoom16

13.Display the parameter values: click on the function icon and, in the editor, add the expression Iif(Any(?RoleId) == False, GetLocalizedString('All'), GetDisplayText(?RoleId)). Add an expression for the second parameter in the same way.

en_rep_designer-sql_expression-editor-icon_01_zoom16

en_rep_designer-sql_parameter-value-expression_01_zoom16

14.Check the result.

en_rep_designer-sql_preview-final_02_zoom16

Back to Report creation in the designer.