Recursive query

Asked

Viewed 5,879 times

4

I need to mount a recursive query to solve a simple problem, but I’m having a little trouble.

I have a table called TABELA1 with the following fields (ID, IDPAI, NOME)

I will put here some examples of records and what I need.

Records in the original table:

Registros na tabela original

How I managed to get them through my query:

USE TESTE
GO

WITH Niveis AS (
                -- Membro âncora
                SELECT Id, IdPai, Nome,
                0 AS Nivel -- nível 0
                FROM TABELA1
                WHERE IdPai IS NULL

                UNION ALL

                -- Filhos
                SELECT T1.Id, T1.IdPai, T1.Nome,
                Nivel+1
                FROM TABELA1 T1
                INNER JOIN Niveis ON T1.IdPai = Niveis.Id
                )
SELECT Id, IdPai, REPLICATE('     ' , Nivel) + Nome AS Nome
FROM Niveis
ORDER BY Id, IdPai

Como eu consegui fazer com que ficassem através da minha query

How I need to:

COMO EU GOSTARIA QUE FICASSEM

Result of the gypsy query, getting close to the solution!

RESULTADO QUERY CIGANO

  • 1

    You can put in your question how is your query until then?

  • Maybe I’d have to use CASE and sub-query. But I don’t think this would be cool for response time. Maybe in your code (should be c# or Vb) you can use a array to group the data (I have no way to provide an example because I don’t know what language you are using)

  • I will use code C#. Thanks for the help, I’ve put the code in the question. ;-)

  • So @Wallacecarlos thought of using an array schema to group this data, because as I said, I believe that using sub-querys for each "loop" within the query can cause a small delay in data delivery (I hope I’m not talking nonsense).

1 answer

2


Well, it seems to me only a matter of adjustment. Just concatenate the previous level name with the current recursion level:

USE TESTE
GO

WITH Niveis AS (
                -- Membro âncora
                SELECT Id, IdPai, convert(varchar(1000), Nome) as Nome,
                0 AS Nivel -- nível 0
                FROM TABELA1
                WHERE IdPai IS NULL

                UNION ALL

                -- Filhos
                SELECT T1.Id, T1.IdPai, convert(varchar(1000), Niveis.Nome + ' - ' + T1.Nome) as Nome,
                Nivel+1
                FROM TABELA1 T1
                INNER JOIN Niveis ON T1.IdPai = Niveis.Id
                )
SELECT Id, IdPai, Nome
FROM Niveis
ORDER BY Id, IdPai

EDIT

I forced the typing and column name to avoid the error reported by comment. I also put the conversion with 1000 characters to prevent the column gets too small and levels do not appear.

  • Gypsy, thank you for the tmb force, but that doesn’t work. Gives the following error: Types do not match between the anchor and the recursive part of the "Name" column of the recursive query "Levels"

  • 1

    @Wallacecarlos I edited the answer.

  • We are very close to the solution my dear friend, take a look at the result: 1 NULL NIVEL 1 2 1 NIVEL 1 - NIVEL 2 3 2 NIVEL 1 - NIVEL 2 - NIVEL 3 4 3 NIVEL 1 - NIVEL 2 - NIVEL 3 - 5 4 NIVEL 1 - NIVEL 2 - NIVEL 3 - 6 NULL QQ COISA 7 NULL OTHER LEVEL 1 8 7 OTHER LEVEL 1 - OTHER LEVEL 2 9 8 OTHER LEVEL 1 - OTHER LEVEL 2

  • @Wallacecarlos What’s missing? That comment was deformed. I can’t tell the difference.

  • Really... I’ll put it up there in question.. 1 min

  • Gypsy, take a look at the end of the question please..

  • @Wallacecarlos Now it’s very simple. It’s just the conversion that got small. I’ve already edited again

  • Sensational.. was exactly that my friend! Thank you very much!!

  • @Wallacecarlos See here how to mark the answer as accepted: http://answall.com/tour. Thank you!

  • Marked friend.. I thank you!! Abs.

  • Folks, if I may I would like to ask one more question, the way I had asked (question query) the ordering only works if the ID’s are increasing, how could I do when the ID’s are out of order at the rising levels? I hope you understand the question. I thank you in advance.

  • @Wallacecarlos You can ask another question, linking this to the new question?

  • I can, but I still can’t link. I’m learning how to use the tool. Thanks for the help.

  • @Wallacecarlos Just paste the question link inside the question ;)

  • The ok.. blz... I thought I could do it by the stackoverflow itself. Flw. Doing this.

  • http://answall.com/questions/52381/sort%C3%A7%C3%A3o-em-query-recursiva... New question!

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.