Use advanced sorting methods in Visual SQL
When creating queries in visual mode, Atlassian Analytics creates the ORDER BY
clause of the generated SQL query by listing the categorical columns in the order that they're chosen in the query and sorts each column in ascending order by default. Aggregated columns are not included in the autogenerated query, but you can add them and change the column sort order and directions by editing “Query sort” in the advanced drawer. For instance, if you wanted to sort by the aggregated column first and then by the categorical columns, you could set that in the “Query sort”.
You also have a couple of other options for performing advanced sorting on your result set.
Ascending/descending sorting
SQL mode
Switch your chart to SQL mode and edit the SQL to change the order of the sorted columns in the ORDER BY
clause.
Updating the SQL query in SQL mode will disconnect visual mode and SQL mode for the chart. In other words, making subsequent changes to your dataset in visual mode after editing the SQL mode query will no longer update the auto-generated query in SQL mode.
Sort rows in the pipeline
Add a “Sort rows” step in Visual SQL, which allows you to re-sort the columns in your result set in any order and direction. This step performs a nested sort, just like the type of sort you can apply in the query.
Custom sorting
Transpose and reorder
Another quick option would be to leverage the “Transpose” and “Reorder columns” steps.
Use a “Transpose” step to swap the rows for columns and columns for rows.
Once transposed, use a “Reorder columns” step to drag your columns (i.e., your rows) in the desired order.
Finally, add one last “Transpose” step to revert your columns back to rows.
The example above leverages the transpose-reorder-transpose method to make the statuses of ‘Open', ‘Closed’, and 'Resolved’ the first three rows of data in the chart.
Create a sort column using a CASE statement
If you have a specific order for values in a column (that can’t be done with a simple ascending or descending sort direction), you can create a new temporary column that uses a CASE
statement to assign a sort priority to each row.
Add a “Formula column” step, select Custom as the formula type, then enter your
CASE
statement. For this example, we want to apply a custom sort order to our "Status" column, where ‘Open’ should be first, ‘Closed’ should be second, ‘Resolved’ should be third, and any other status would be ordered last.CASE WHEN "Status" = 'Open' THEN 1 WHEN "Status" = 'Closed' THEN 2 WHEN "Status" = 'Resolved' THEN 3 ELSE 4 END
- After creating the new column, you’d then use the “Sort rows” step to apply the desired sort order to your columns.
- Finally, you can hide the temporary column from your result set.