Query help for the problem created?

Asked

Viewed 46 times

-1

I need to create a list composed by the fields Current and Next Stage both stage names coming from the same table.

Example of the result I seek:

Tabela desejada

Stage Table (stage name provider)

Tabela fornecedora do nome do estágio

The query searches the Stage Table (below) the stage names in the Stages table (above):

Tabela EstagiosCronologia

I can even have the same ID for Current and Next.

    SELECT e.Estagio AS Atual, 
    (SELECT e.Estagio FROM EstagiosCronologia ec 
    INNER JOIN Estagios e ON ec.ProximoEstagioId = e.EstagioId)  AS Proximo 
    FROM EstagiosCronologia ec 
    INNER JOIN Estagios e ON ec.EstagioAtualId = e.EstagioId

This query above works for only one item - with the table Stage Ronology polluted returns me an error:

Msg 512, Level 16, State 1, Line 2 Subquery returned more than 1 value. This is not permitted when the subquery Follows =, != , <, <= , >, >= or when the subquery is used as an Expression.

How to create this query?

Table Interns

CREATE TABLE [dbo].[EstagiosCronologia] (
        [CronologiaId]     VARCHAR (36) NOT NULL,
        [EstagioAtualId]   VARCHAR (36) NOT NULL,
        [ProximoEstagioId] VARCHAR (36) NOT NULL,
        PRIMARY KEY CLUSTERED ([CronologiaId] ASC),
        CONSTRAINT [FK_EstagiosCronologia_Estagios] FOREIGN KEY ([EstagioAtualId]) REFERENCES [dbo].[Estagios] ([EstagioId]) ON DELETE CASCADE
    );

Table Stages

CREATE TABLE [dbo].[Estagios] (
    [EstagioId]   VARCHAR (36)    NOT NULL,
    [CursoId]     VARCHAR (36)    NOT NULL,
    [Estagio]     VARCHAR (50)    NOT NULL,
    [Duracao]     INT             NOT NULL,
    [ValorTabela] DECIMAL (10, 2) NOT NULL,
    PRIMARY KEY CLUSTERED ([EstagioId] ASC),
    CONSTRAINT [FK_Estagios_Cursos] FOREIGN KEY ([CursoId]) REFERENCES [dbo].[Cursos] ([CursoId]) ON DELETE CASCADE
);

Dados Cronologia

INSERT INTO [dbo].[EstagiosCronologia] ([CronologiaId], [EstagioAtualId], [ProximoEstagioId]) VALUES (N'7027a044-9530-446f-a60d-9971a206d452', N'508ccd5b-b177-4876-9b7c-987f64ada5ef', N'bb84bd20-4907-4a92-815f-44c08b24410a')
INSERT INTO [dbo].[EstagiosCronologia] ([CronologiaId], [EstagioAtualId], [ProximoEstagioId]) VALUES (N'dc72f06c-470b-4913-b480-4106e05831b4', N'bb84bd20-4907-4a92-815f-44c08b24410a', N'c86516bc-bae2-4edf-9c75-236cdc5fccd8')

Data Interns

INSERT INTO [dbo].[Estagios] ([EstagioId], [CursoId], [Estagio], [Duracao], [ValorTabela]) VALUES (N'508ccd5b-b177-4876-9b7c-987f64ada5ef', N'dc72f06c-470b-4913-b480-4106e05831b4', N'Teste', 40, CAST(1589.00 AS Decimal(10, 2)))
INSERT INTO [dbo].[Estagios] ([EstagioId], [CursoId], [Estagio], [Duracao], [ValorTabela]) VALUES (N'bb84bd20-4907-4a92-815f-44c08b24410a', N'dc72f06c-470b-4913-b480-4106e05831b4', N'Nursery Play', 40, CAST(1950.00 AS Decimal(10, 2)))
INSERT INTO [dbo].[Estagios] ([EstagioId], [CursoId], [Estagio], [Duracao], [ValorTabela]) VALUES (N'c86516bc-bae2-4edf-9c75-236cdc5fccd8', N'dc72f06c-470b-4913-b480-4106e05831b4', N'Teste 2', 40, CAST(1500.00 AS Decimal(10, 2)))
  • Each internship can only have one next?

  • no, each stage may have more than one next.

  • The error ta happening pq the subquery returns more than one line, basically you ta tried to put 2 or more records within one, if each stage can only have one Próximo there is something wrong with your table EstagiosCronologia

  • Can you pass me the creation scripts of the tables with some record for me to test?

  • Help like this - this way?

  • adds to question, if they will not delete

  • I answered, if that’s not what you want let me know

  • Sorry Gabriel Oliveira, I don’t see the answer anywhere... I’m new to Stackoverflow. How do I see your answer?

Show 3 more comments

2 answers

0

I got the answer.

SELECT e.Estagio AS Atual, (SELECT est.Estagio FROM EstagiosCronologia ecs INNER JOIN Estagios est ON ecs.ProximoEstagioId = est.EstagioId WHERE ecs.EstagioAtualId = ec.EstagioAtualId) As Proximo
FROM EstagiosCronologia ec 
INNER JOIN Estagios e ON ec.EstagioAtualId = e.EstagioId

0

Very logical. Simple.

SELECT ec.*, ea.Estagio AS EstagioAtual, pe.Estagio As ProximoEstagio  
FROM EstagiosCronologia ec
JOIN Estagios ea ON ea.EstagioId = ec.EstagioAtualId 
JOIN Estagios pe ON pe.EstagioId = ec.ProximoEstagioId 

Answering my own question.

Browser other questions tagged

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