There is a table ACCOUNT_SCENARIO:

ACCOUNT_SCENARIO

In which there is a column ACCOUNT_TYPE, which reflects the hierarchy, of which only 3 - 'CE', 'CF', 'SP'.

Is it possible to sort the table first for the first hierarchy, then for the second and at the end for the 3rd? At the same time, each subsequent sorting should not affect the previous one.

But actually the sorting algorithm itself (sorted by a column from another table under the name ACCOUNT_HIERARCHY):

SELECT ACSC.* FROM ACCOUNT_SCENARIO AS ACSC JOIN (SELECT PARENT_ACCOUNT_ID, MIN(EXCEL_ORDER) AS EXCEL_ORDER FROM ACCOUNT_HIERARCHY WHERE ACCOUNT_HIERARCHY_ID = 'SP' GROUP BY PARENT_ACCOUNT_ID ) X ON ACSC.ACCOUNT_ID = X.PARENT_ACCOUNT_ID WHERE ACCOUNT_TYPE = 'SP' ORDER BY ACCOUNT_TYPE, X.EXCEL_ORDER, ACCOUNT_DATE 
  • one
    Of course it is possible, inside order by write the sorting function, with comparisons with the case when then else end with division multiplications, sort the branches in any order. - nick_n_a
  • I forgot to add - with subqueries. (eg order by (select a from table1 where table1.id = x.id) - nick_n_a
  • @nick_n_a sorry for arrogance, but can you reset an example of such sorting? I know how to google, but I'm afraid to find what I need. - Anton Turcanu
  • one
    On the question, it is not entirely clear what exactly you need, but for the "three cases", I would write the following order by coalesce(case when ACCOUNT_TYPE='CE' then expression_1 end, case when ACCOUNT_TYPE='CF' then expression_2 end, case when ACCOUNT_TYPE='SP' then expression_3 end ) I showed the subqueries. Next - try - succeed. - nick_n_a
  • @nick_n_a I just can't understand what I'm doing wrong: pastebin.com/yGDfDkGe - Anton Turcanu

0