Query hierarchical - How to summarize values

Tuning de Banco, Tuning de SQL, Ferramentas de tuning
Post Reply
sp66d_rac6r
Rank: Estagiário Sênior
Rank: Estagiário Sênior
Posts: 9
Joined: Tue, 02 Dec 2008 10:43 am
Location: Sorocaba-SP

Hello everyone,

I have a query that returns the data below and need to summarize using the father x child concept, but the values ??are only at the lowest levels (last children).

I have maximum 4 levels of hierarchy.

How do I summarize the respective parents?

I need this return on a query.

Select all

rating|id_chield|id_parent|VALUE 
A|1|null|NULL| 
A|2|1|NULL 
A|3|2|NULL| 
A|4|3|NULL| 
A|5|3|100| 
A|6|3|NULL| 
.... 
B|1|null|NULL| 
B|2|1|NULL| 
B|3|2|NULL| 
B|4|3|50| 
B|5|3|NULL| 
B|6|3|60|


Thanks in advance.

ABS.
burga
Rank: DBA Pleno
Rank: DBA Pleno
Posts: 232
Joined: Thu, 26 Nov 2009 1:05 pm
Location: SP
Ricardo H. Tajiri

Use CONNECT BY PRIOR but for us to be able to help you better, you are good.
give examples of your environment as well. Table, sample data, etc ...
sp66d_rac6r
Rank: Estagiário Sênior
Rank: Estagiário Sênior
Posts: 9
Joined: Tue, 02 Dec 2008 10:43 am
Location: Sorocaba-SP

I have a hierarchy table:

Select all

ID_CHIELD	|	ID_PARENT	|	HIERARQUIA 
1	|		|	DIRETOR 
2	|	1	|	GERENTE REGIÃO 1 
3	|	2	|	LÍDER 1 REGIÃO 1 
4	|	3	|	Funcionario 1 
5	|	3	|	Funcionario 2 
6	|	2	|	LÍDER 2 REGIÃO 1 
7	|	6	|	Funcionario 3 
8	|	6	|	Funcionario 4 
9	|	6	|	Funcionario 5 
10	|	6	|	Funcionario 6 
11	|	6	|	Funcionario 7 
..	 
31	|	1	|	GERENTE REGIÃO 2 
32	|	31	|	Funcionario 8 
33	|	31	|	Funcionario 9 
34	|	31	|	Funcionario 10 
35	|	31	|	Funcionario 11 
36	|	31	|	Funcionario 12 
37	|	31	|	Funcionario 13 
38	|	31	|	Funcionario 14

Another table with the sales of the officials (notice that the value is only with the employee and with its "parents"):

Select all

ID_FUNCIONARIO (ID_CHIELD)	|	VENDA 
33	|	333797300 
34	|	41204604.3 
36	|	116024774.4 
37	|	613123.65
I am running a join between them with Connect BY Prior (notem that again the values ??are only with the employee and not With its "parents"):

Select all

ID_CHIELD	|	ID_PARENT	|	HIERARQUIA	|		|	LEVEL (CONNECT BY …)	|	TOTAL VENDA 
1	|		|	DIRETOR	|	DIRETOR/	|	1	|	 
2	|	1	|	GERENTE REGIÃO 1	|	DIRETOR/GERENTE REGIÃO 1	|	2	|	 
3	|	2	|	LÍDER 1 REGIÃO 1	|	DIRETOR/GERENTE REGIÃO 1/LÍDER 1 REGIÃO 1	|	3	|	 
4	|	3	|	Funcionario 1	|	DIRETOR/GERENTE REGIÃO 1/LÍDER 1 REGIÃO 1/Funcionario 1	|	4	|	 
5	|	3	|	Funcionario 2	|	DIRETOR/GERENTE REGIÃO 1/LÍDER 1 REGIÃO 1/Funcionario 2	|	4	|	 
6	|	2	|	LÍDER 2 REGIÃO 1	|	DIRETOR/GERENTE REGIÃO 1/LÍDER 2 REGIÃO 1	|	3	|	 
7	|	6	|	Funcionario 3	|	DIRETOR/GERENTE REGIÃO 1/LÍDER 1 REGIÃO 1/Funcionario 3	|	4	|	 
8	|	6	|	Funcionario 4	|	DIRETOR/GERENTE REGIÃO 1/LÍDER 1 REGIÃO 1/Funcionario 4	|	4	|	 
9	|	6	|	Funcionario 5	|	DIRETOR/GERENTE REGIÃO 1/LÍDER 1 REGIÃO 1/Funcionario 5	|	4	|	 
10	|	6	|	Funcionario 6	|	DIRETOR/GERENTE REGIÃO 1/LÍDER 1 REGIÃO 1/Funcionario 6	|	4	|	 
11	|	6	|	Funcionario 7	|	DIRETOR/GERENTE REGIÃO 1/LÍDER 1 REGIÃO 1/Funcionario 7	|	4	|	 
	|		|		|		|		|	 
	|		|		|		|		|	 
31	|	1	|	GERENTE REGIÃO 2	|	DIRETOR/GERENTE REGIÃO 2/	|	2	|	 
32	|	31	|	Funcionario 8	|	DIRETOR/GERENTE REGIÃO 2/Funcionario 8	|	3	|	 
33	|	31	|	Funcionario 9	|	DIRETOR/GERENTE REGIÃO 2/Funcionario 9	|	3	|	333797300 
34	|	31	|	Funcionario 10	|	DIRETOR/GERENTE REGIÃO 2/Funcionario 10	|	3	|	41204604.3 
35	|	31	|	Funcionario 11	|	DIRETOR/GERENTE REGIÃO 2/Funcionario 11	|	3	|	 
36	|	31	|	Funcionario 12	|	DIRETOR/GERENTE REGIÃO 2/Funcionario 12	|	3	|	116024774.4 
37	|	31	|	Funcionario 13	|	DIRETOR/GERENTE REGIÃO 2/Funcionario 13	|	3	|	613123.65 
38	|	31	|	Funcionario 14	|	DIRETOR/GERENTE REGIÃO 2/Funcionario 14	|	3	|
I have tried almost everything, I can not "rise" the blessed values ??for their superiors.
lmendes.cps
Rank: Estagiário Sênior
Rank: Estagiário Sênior
Posts: 13
Joined: Mon, 20 Jun 2011 2:14 pm
Location: Campinas - SP

Friend, according to what I understand from your example, I did the following query:

Select all

select id_parent, id_chield, hierarquia, sum(vendas) over (partition by id_parent) as vendas 
  from (select s.id_parent,s.id_chield,s.hierarquia, nivel, nvl(v.venda,0) as vendas 
          from vendas v,  
               (select id_parent,  
                       id_chield,  
                       hierarquia,  
                       level as nivel 
                  from hierarquia h 
                  start with h.id_chield = 1 
                connect by prior id_chield = id_parent) s 
         where s.id_chield = v.id_chield(+));
I hope I have helped.
User avatar
fbifabio
Moderador
Moderador
Posts: 199
Joined: Tue, 22 Feb 2011 1:51 pm
Location: São Paulo - SP
Contact:

Take a look at my http://www.fabioprado.net/2011/06/consu ... eries.html. Cho that he can help you write a query to return what you need! I also explain about how to use hierarchical query in my tuning training http://www.fabioprado.net/p/treinamentos.html).
[]

Fábio Prado www.fabioprado.net
Maioli
Rank: Estagiário Júnior
Rank: Estagiário Júnior
Posts: 1
Joined: Wed, 08 Sep 2010 11:49 am
Location: São Paulo-SP

See the example in https://www.linkedin.com/post/edit/plsq ... ADs-maioli In this article I explain in detail and with examples in PL / SQL.
Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests