Первоначальной мотивацией для этого было отображение фактических планов выполнения Oracle, сохраненных в GV$SQL_PLAN, в визуальном и ясном виде.
- I've attached my suggested solutions.
- Please feel free to add yours, as long as it fulfills the requirements.
- Укажите имя базы данных, для которой было написано ваше решение.
Требования
Ввод
- Таблица, содержащая столбцы «id» (идентификатор узла) и «pid» (идентификатор родителя узла).
Вывод
- The result should be a an ASCII art graph (see example below)
- Each pair of "id" and "pid" nodes should be connected with an edge.
- Корневой узел может иметь дополнительное одиночное ребро.
- Не должно быть других ребер, особенно ребер, которые не соединены ни с одним узлом одной из своих сторон.
Код
- A single SELECT statement based only on native SQL
- No UDF (User Defined Functions).
- Нет T-SQL, PL/SQL и т.д.
Пример данных
create table h (id int,pid int);
insert into h (id,pid) values (0 ,null);
insert into h (id,pid) values (1 ,0 );
insert into h (id,pid) values (2 ,1 );
insert into h (id,pid) values (3 ,2 );
insert into h (id,pid) values (4 ,3 );
insert into h (id,pid) values (5 ,4 );
insert into h (id,pid) values (6 ,3 );
insert into h (id,pid) values (7 ,6 );
insert into h (id,pid) values (8 ,7 );
insert into h (id,pid) values (9 ,8 );
insert into h (id,pid) values (10 ,9 );
insert into h (id,pid) values (11 ,10 );
insert into h (id,pid) values (12 ,9 );
insert into h (id,pid) values (13 ,12 );
insert into h (id,pid) values (14 ,8 );
insert into h (id,pid) values (15 ,6 );
insert into h (id,pid) values (16 ,15 );
insert into h (id,pid) values (17 ,6 );
insert into h (id,pid) values (18 ,17 );
insert into h (id,pid) values (19 ,17 );
insert into h (id,pid) values (20 ,3 );
insert into h (id,pid) values (21 ,20 );
insert into h (id,pid) values (22 ,21 );
insert into h (id,pid) values (23 ,22 );
insert into h (id,pid) values (24 ,21 );
Полученные результаты
Вертикальные братья и сестры
|
|____ 1
|
|____ 2
|
|____ 3
|
|____ 4
| |
| |____ 5
|
|____ 6
| |
| |____ 7
| | |
| | |____ 8
| | |
| | |____ 9
| | | |
| | | |____ 10
| | | | |
| | | | |____ 11
| | | |
| | | |____ 12
| | | |
| | | |____ 13
| | |
| | |____ 14
| |
| |____ 15
| | |
| | |____ 16
| |
| |____ 17
| |
| |____ 18
| |
| |____ 19
|
|____ 20
|
|____ 21
|
|____ 22
| |
| |____ 23
|
|____ 24
Горизонтальные братья и сестры
|
|
|
0
|
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
|
3
|
|
---------------------------------------
| | |
| | |
4 6 20
| | |
| | |
| ------------------- |
| | | | |
| | | | |
5 7 15 17 21
| | | |
| | | |
| | ------ ------
| | | | | |
| | | | | |
8 16 18 19 22 24
| |
| |
-------- |
| | |
| | |
9 14 23
|
|
------
| |
| |
10 12
| |
| |
| |
| |
| |
11 13