0

Given a table 'TEMPLATES'(with columns ID,NAME,IS_MASTER,MASTER_ID etc) which describes multiple content management system elements e.g. masters, templates etc. The column IS_MASTER filters the master-docs. I want to filter all master-docs and the associated elements(documents), which point to the master over the 'MASTER_ID' column of the same table but I want to order the rows one after another for example after a result row which shows a master should be listed the elements, which point to this master (and not all rows mixed up like in the screenshot) :

enter image description here

But I can only do it in this way, I don't know how to order or group by

SELECT x.*,x.ROWID FROM TEMPLATE x
WHERE IS_MASTER IN (1) OR MASTER_ID IS NOT NULL 

1 Answer 1

1

You could do this:

select *
from templates
order by nvl(master_id, id);

This would tell the database to sort by master_id, and if that column is empty (NULL) to use the id. This way the master and its children are sorted together.

Alternatively you can use a hierarchical query:

select *
from templates
start with master_id is null
connect by master_id = prior id;
1
  • Thank you, I didn't know either one or the other functions, but the second query orders the rows in the desired way! The first makes it 90%.
    – user615993
    Aug 25, 2021 at 12:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.